Unable to filter on field on edit.php page


I’m borrowing from the code at Add filter menu to admin list of posts (of custom type) to filter posts by custom field values to add a filter to my custom post type with the parse_query filter:

function grievance_posts_filter( $query ){
    global $pagenow;
    $type = 'post';
    if (isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
    if ( 'grievance' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['step']) && $_GET['step'] != '') {
        $query->query_vars['meta_key'] = 'my_first_plugin_fields[step]';
        $query->query_vars['meta_value'] = 2;
add_filter( 'parse_query', 'grievance_posts_filter' );

In the above code, I have hard-coded the value of the the meta_value to troubleshoot. I expect the above code to show me only the posts with the step value is set to “2”. However, instead of filtering the posts, the edit.php page is not showing any posts at all. I don’t think I’m using the right values for the meta_key and meta_value elements. I’m not sure what to put there.

I have also tried:

        $query->query_vars['meta_key'] = 'my_first_plugin_fields';
        $query->query_vars['meta_value'] = array( 'step' => 2 );

but this did not work either.

, , , StevieD 3 years 2019-10-29T01:32:31-05:00 0 Answers 81 views 0

Leave an answer