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. My meta_key has multiple sub-keys associated with it.
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.
I also attempted:
$meta = [ 'my_first_plugin_fields' => [ 'key' => 'step', 'value' => '2', 'compare' => '=' ] ] ;
$query->query_vars['meta_query'] = $meta;
without success.