• HOME
  • NEWS
  • PORTFOLIO
  • LAB
  • ABOUT
  • CONTACT

Woocommerce membership plugin : include custom field in admin search

categories : programmation

by Trân on March 17, 2020


Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 118

Notice: A non well formed numeric value encountered in /home/tigerbal/www/wp-content/plugins/crayon-syntax-highlighter/crayon_formatter.class.php on line 119

I had a hard time finding how to add custom field to admin search under woocommerce > members list. The woocommerce membership plugin is in fact using the ‘posts_clauses’ filter to filter the search result. So to add custom fields, here is the code I use :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
function advanced_admin_custom_search($pieces, WP_Query $wp_query) {
    global $wpdb;
 
    if (!is_admin() ) return $pieces;
 
    if (!array_key_exists('post_type', $wp_query->query)) return $pieces;
 
    // bail out if not the correct post type
    if ( 'wc_user_membership' === $wp_query->query['post_type'] ) {
      
        // $wp_query->set( 'post_type',  'wc_user_membership' );
 
        // search
        if ( isset( $wp_query->query['s'] ) ) {
            
            // add here your acf fields
            $list_searcheable_acf = array("my_acf_field_name");
 
            // get search expression
            $terms = $wp_query->query_vars[ 's' ];
                    
            // explode search expression to get search terms
            $exploded = explode( ' ', $terms );
            if( $exploded === FALSE || count( $exploded ) == 0 )
                $exploded = array( 0 => $terms );
                
            // reset search in order to rebuilt it as we whish
            $where = $pieces['where'];
            
            foreach( $exploded as $tag ) :
                $where .= "
               OR EXISTS (
                    SELECT * FROM " . $wpdb->postmeta . "
                        WHERE post_id = " . $wpdb->posts . ".ID
                            AND " . $wpdb->posts . ".post_type = 'wc_user_membership'
                            AND (";
                foreach ($list_searcheable_acf as $searcheable_acf) :
                    $where .= " (meta_key LIKE '%" . $searcheable_acf . "%' AND meta_value REGEXP '[[:<:]]{$tag}[[:>:]]') ";
                endforeach;
                    $where .= ")
                )";
            endforeach;
 
            // replace the where clauses
            $pieces['where'] = $where;
        }
    }
 
return $pieces;
}
 
 
add_filter( 'posts_clauses', 'advanced_admin_custom_search', 500, 2 );
 
 

Using parcel-plugin-custom-dist-structure with parcel » « L’Association

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

 
 

© fruit du dragon - freelance creative coder

Categories

  • général
  • inspiration
  • lab
  • programmation
  • sites
  • work

Tags

android architecture archives canvas cellules-studio cornelius css culturel demonstrateur design eshop flash fremok html html/css/js hugo IFTTT incandescence Instagram institutionnel ios ipad jeux js krpano luxe media design mobile musée nomoon paper fold photographie requins-marteaux responsive sbam snippet tablette toute-attente tran nn transport typographie ui ultranoir woocommerce wordpress

Stats


Notice: Undefined variable: extra in /home/tigerbal/www/wp-content/plugins/pulsemaps/pm-widget.php on line 47

Archives

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org