Title: Filtering problem
Last modified: May 31, 2023

---

# Filtering problem

 *  Resolved [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * (@israelmeirwi)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/)
 * When I do a filter based on ACF fields on the first click I get nothing
 * But if I click one more time the correct results are displayed

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Plugin Author [Stefan van den Dungen Gronovius](https://wordpress.org/support/users/dungengronovius/)
 * (@dungengronovius)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16782037)
 * Can you try to disable our plugin and try to filter on a default WordPress filter
   and see if you still have this same issue? I believe there is another search 
   plugin that has this behavior by default.
 *  Thread Starter [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * (@israelmeirwi)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16792136)
 * Yes I have this code
 *     ```wp-block-code
       function cf_search_join( $join ) {
           global $wpdb;
   
           if ( is_search() ) {    
               $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
           }
   
           return $join;
       }
       add_filter('posts_join', 'cf_search_join' );
   
   
   
       function cf_search_where( $where ) {
         global $pagenow, $wpdb;
   
          if ( is_search() ) {
             $where = preg_replace(
               "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
               "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
           }
   
          return $where;
          }
       add_filter( 'posts_where', 'cf_search_where' );
       ```
   
 * It helps me in searching in META FILDLS. Is there a better way to do it?
 *  Plugin Author [Stefan van den Dungen Gronovius](https://wordpress.org/support/users/dungengronovius/)
 * (@dungengronovius)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16794067)
 * I don’t believe this triggers the actual behavior that you’re having. When you
   disable these two hooks, does it work as expected? And did you try to deactivate
   our plugin see if you’re still able to reproduce the issue without our plugin?
 * Aside from that, I recommend giving your join an Alias (same for where of course)
   in order to prevent conflicts with other plugins that alter the query as well.
 *  Thread Starter [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * (@israelmeirwi)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16794117)
 * Yes I checked it does the problem
   I changed this code but it doesn’t help it 
   only happens in the ACF field
 *     ```wp-block-code
       function cf_search_join( $join ) {
           global $wpdb;
   
           // בדיקה האם יש סינון של Admin Columns
           if (!is_admin_columns_filter() || !is_acf_filter()) {
               if ( is_search() ) {    
                   $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . '.ID = ' . $wpdb->postmeta . '.post_id ';
               }
           }
   
           return $join;
       }
       add_filter('posts_join', 'cf_search_join' );
   
       function cf_search_where( $where ) {
           global $pagenow, $wpdb;
   
           // בדיקה האם יש סינון של Admin Columns
           if (!is_admin_columns_filter() || !is_acf_filter()) {
               if ( is_search() ) {
                   $where = preg_replace(
                       "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                       "(".$wpdb->posts.".post_title LIKE $1) OR (".$wpdb->postmeta.".meta_value LIKE $1)", $where );
               }
           }
   
           return $where;
       }
       add_filter( 'posts_where', 'cf_search_where' );
   
       function is_admin_columns_filter() {
           $filters = array(
               'cpac/filter/columns',
               'cpac/filter/terms',
           );
   
           return in_array(current_filter(), $filters);
       }
       function is_acf_filter() {
           if (current_filter() == 'acf/load_field') {
               $field = func_get_arg(0);
               if ($field['key'] == 'field_5fda152076292') {
                   return true;
               }
           }
           return false;
       }
       ```
   
 * There may be another way to make the search search ACF fields as well
    -  This reply was modified 3 years ago by [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/).
 *  Plugin Author [Stefan van den Dungen Gronovius](https://wordpress.org/support/users/dungengronovius/)
 * (@dungengronovius)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16794811)
 * Are you able to debug the difference in the request for the first time and the
   second time you filter the page?
 * Maybe you could install the plugin ‘Query Monitor’ and test if there is an error
   in the query the first or second time.
 *  Thread Starter [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * (@israelmeirwi)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16794960)
 * I installed it is the error it raises
 * Not unique table/alias: ‘fcm_postmeta’
 *  Plugin Author [Stefan van den Dungen Gronovius](https://wordpress.org/support/users/dungengronovius/)
 * (@dungengronovius)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16796685)
 * OK, so you can add an alias for your join as I mentioned before.
 * Something like:
 * LEFT JOIN’ . $wpdb->postmeta . ‘ AS prefix_pm
 * And of course in your where clauses also use the alias instead of the direct 
   table name.
 *  Thread Starter [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * (@israelmeirwi)
 * [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16796864)
 * I made a few more games this is my code
 *     ```wp-block-code
       function cf_search_join( $join ) {
           global $wpdb;
   
           // בדיקה האם יש סינון של Admin Columns
           if (!is_admin_columns_filter() || !is_acf_filter()) {
               if ( is_search() ) {    
                   $join .= ' LEFT JOIN ' . $wpdb->postmeta . ' AS prefix_pm ON ' . $wpdb->posts . '.ID = prefix_pm.post_id ';
               }
           }
   
           return $join;
       }
       add_filter('posts_join', 'cf_search_join' );
   
       function cf_search_where( $where ) {
           global $pagenow, $wpdb;
   
           // בדיקה האם יש סינון של Admin Columns
           if (!is_admin_columns_filter() || !is_acf_filter()) {
               if ( is_search() ) {
                   $where = preg_replace(
                       "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                       "(".$wpdb->posts.".post_title LIKE $1) OR (prefix_pm.meta_value LIKE $1)", $where );
               }
           }
   
           return $where;
       }
       add_filter( 'posts_where', 'cf_search_where' );
   
   
   
       function is_admin_columns_filter() {
           $filters = array(
               'cpac/filter/columns',
               'cpac/filter/terms',
           );
   
           return in_array(current_filter(), $filters);
       }
       function is_acf_filter() {
           if (current_filter() === 'acf/load_field') {
               $field = func_get_arg(0);
               if ($field['key'] === 'field_5fda152076292') {
                   return true;
               }
           }
           return false;
       }
       ```
   
 * And it works great

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Filtering problem’ is closed to new replies.

 * ![](https://ps.w.org/codepress-admin-columns/assets/icon.svg?rev=1521754)
 * [Admin Columns](https://wordpress.org/plugins/codepress-admin-columns/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/codepress-admin-columns/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/codepress-admin-columns/)
 * [Active Topics](https://wordpress.org/support/plugin/codepress-admin-columns/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/codepress-admin-columns/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/codepress-admin-columns/reviews/)

 * 8 replies
 * 2 participants
 * Last reply from: [israel wissotzky](https://wordpress.org/support/users/israelmeirwi/)
 * Last activity: [3 years ago](https://wordpress.org/support/topic/filtering-problem-4/#post-16796864)
 * Status: resolved