• Resolved israel wissotzky

    (@israelmeirwi)


    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

    (@dungengronovius)

    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

    (@israelmeirwi)

    Yes I have this 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

    (@dungengronovius)

    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

    (@israelmeirwi)

    Yes I checked it does the problem

    I changed this code but it doesn’t help it only happens in the ACF field

    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

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    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

    (@israelmeirwi)

    I installed it is the error it raises

    Not unique table/alias: ‘fcm_postmeta’

    Plugin Author Stefan van den Dungen Gronovius

    (@dungengronovius)

    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

    (@israelmeirwi)

    I made a few more games this is my 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.