I’ve a custom post type with some custom meta box. I want to filter my custom post type posts using custom meta value. I’ve written the code below. But it return no post found. Can anyone tell me where I did wrong? Here is the codes.

<?php

add_action('restrict_manage_posts','restrict_listings_by_metavalue');
function restrict_listings_by_metavalue() {
    global $typenow;
    global $wp_query;
    if ($typenow=='jspp_std') {
        echo '<input type="text" name="adate" id="adate" placeholder="Enter Admission date" />';
    }
}
add_filter('parse_query','jspp_custom_meta_query');
function jspp_custom_meta_query($query) {
    global $typenow;
    global $pagenow;

    if( $pagenow == 'edit.php' && $typenow == 'jspp_std' && isset($_GET['adate']) )
    {

        $query->query_vars['meta_key'] = '_jspp_sp_sid';
        $query->query_vars['meta_value'] = $_GET['adate'];
        $query->query_vars['meta_compare'] = '=';

    }
}

?>

Screenshot of result it return.Thanks in advance.

enter image description here

2 Answers
2

I ave faced the same issue when I was trying to filter custom-post-type with custom-field
but I have resolved this issue in the following steps.
Converted custom-field name from writer to _writer
and then I have updated following code inside callback function of parse_query hook that I can add custom-field meta_query like

$query->set( 'meta_query', array(
    array(
          'key'     => '_writer',
           'compare' => '=',
           'value'   => $_GET['_writer'],
           'type'    => 'numeric',
      )
) );         

This Above Solution Worked for me.
Documentation https://developer.wordpress.org/reference/hooks/pre_get_posts/
Helpful Links https://developer.wordpress.org/reference/hooks/pre_get_posts/#comment-2571
https://stackoverflow.com/questions/47869905/how-can-i-filter-records-in-custom-post-type-list-in-admin-based-on-user-id-that

Leave a Reply

Your email address will not be published. Required fields are marked *