• Resolved iyuvalk

    (@iyuvalk)


    Hi,

    I have installed WordPress and the Advanced Custom Fields plugin (ACF) in order to use it as an internal CMS system (this is why I can’t specify a link to the web page). I used the ACF configuration to create several custom fields and then added code based on the example here: https://activelab.io/tutorials/filtering-custom-post-types-by-post-meta-in-the-wordpress-admin-area to add the ability to filter the posts of a certain type based on the value in one of the custom fields. The dropdown box contains all the options and when I select any of the options in it it filters the posts but all the options on the dropbox disappear.

    Has anyone here seen anything like it before?

    T.I.A,
    Yuval.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    We do not support Pro versions of plugins in these forums. I realize your question really has nothing to do with ACF, you’re filtering based on meta data. How it got there is irrelevant. Just to avoid potential confusion, I’ve removed Pro references from your topic. It’s new URL is https://www.ads-software.com/support/topic/filtering-posts-based-on-a-custom-field-acf/

    Without seeing your code, it’s hard to say what might be the problem. I’m guessing that the options are coming from posts. When you filter the posts, the other options will not be found. The trick will be to filter the list table query, but not filter the options query.

    Please post your relevant code somewhere like Pastebin orGist and provide the link here.

    Thread Starter iyuvalk

    (@iyuvalk)

    Hi,

    Thanks for the quick reply. I’m not sure that I fully understand what you mean by “filter the list table query, but not filter the options query” (I’m quite new to WordPress programming…), so I have uploaded both my relevant functions and two screenshots that show the posts list before and after the filtering. Here are the links:
    Filtering code: https://pastebin.com/fnYG9gYk
    Screenshots:
    https://www.artifex.co.il/he/wp-content/uploads/2018/05/before_filtering.png
    https://www.artifex.co.il/he/wp-content/uploads/2018/05/after_filtering.png

    Thread Starter iyuvalk

    (@iyuvalk)

    I think that I finally understood what you meant and change the sygnia_filter_best_practices function (which is hooked to the parse_query filter) to this:

    function sygnia_filter_best_practices( $query ) {
    global $pagenow;
    // Get the post type
    //modify the query only if it admin and main query.
    if( !(is_admin() AND $query->is_main_query()) ){
    return $query;
    }

    //we want to modify the query for the targeted custom post and filter option
    if( !(‘best_practice’ === $query->query[‘post_type’] AND isset($_REQUEST[‘pillar_filter’]) ) ){
    return $query;
    }

    $post_type = isset( $_GET[‘post_type’] ) ? $_GET[‘post_type’] : ”;
    if ( is_admin() && $pagenow==’edit.php’ && $post_type == ‘best_practice’ && isset( $_GET[‘pillar_filter’] ) && $_GET[‘pillar_filter’] !=’all’ ) {
    $query->query_vars[‘meta_key’] = ‘pillar’;
    $query->query_vars[‘meta_value’] = $_GET[‘pillar_filter’];
    $query->query_vars[‘meta_compare’] = ‘=’;
    // var_dump($query);
    // error_log(var_export($query, true));
    }
    }

    and now it seems that it works correctly. thanks a lot!
    Now, I have few more questions but to keep everything neat I’ll post them as new tickets.

    Thanks a lot for all your help!
    Yuval

    • This reply was modified 6 years, 6 months ago by iyuvalk.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Filtering posts based on a custom field (ACF)’ is closed to new replies.