admin管理员组

文章数量:1122846

I am trying to add an additional filter to be able to filter by a custom meta value but it is not returning any values when I would expect it to.

Here is my code to add the filter dropdown (this seems to be working as I would expect):

add_action( 'restrict_manage_posts', 'add_admin_event_filters', 10, 1 );
function add_admin_event_filters( $post_type ){
    if( 'finalists' !== $post_type ){
        return;
    }
    $events_args = array(
        'post_type' => 'events',
        'numberposts' => -1,
        'orderby' => 'title',
        'order' => 'ASC',
        'post_parent' => 0,
    );
    $events = get_posts( $events_args );
    if (!$events) {
        return;
    }
    $selected = '';
    $request_attr = 'events';
    if ( isset($_REQUEST[$request_attr]) ) {
        $selected = $_REQUEST[$request_attr];
    }
    ob_start();
    ?>
    <select name="<?php echo $request_attr; ?>">
        <option value="0">All events</option>
        <?php foreach ($events as $event) { ?>
        <option value="<?php echo $event->ID; ?>" <?php selected($event->ID, $selected); ?>><?php echo $event->post_title; ?></option>
        <?php } ?>
    </select>
    <?php
    echo ob_get_clean();
}

Here is the code to modify the query based on what was selected in the dropdown:

add_filter( 'parse_query', 'filter_event_request_query' , 10);
function filter_event_request_query($query){
    //modify the query only if it is admin and main query.
    if( !(is_admin() && $query->is_main_query()) ){ 
      return $query;
    }
    //we want to modify the query for the targeted custom post.
    if( 'finalists' !== $query->query['post_type'] ){
      return $query;
    }
    //type filter
    $request_attr = 'events';
    if( isset($_REQUEST[$request_attr]) &&  0 != $_REQUEST[$request_attr]){
        $query->set('meta_query', array(
            array(
                'key'       => 'award_event_id',
                'compare'   => '=',
                'value'     => $_REQUEST[$request_attr],
            )
        ));
    }
    return $query;
}

Here is a dump of the $query variable:

object(WP_Query)#2286 (50) {
  ["query"]=>
  array(11) {
    ["m"]=>
    string(1) "0"
    ["s"]=>
    string(0) ""
    ["paged"]=>
    string(1) "1"
    ["order"]=>
    string(0) ""
    ["orderby"]=>
    string(0) ""
    ["post_type"]=>
    string(9) "finalists"
    ["events"]=>
    string(5) "96774"
    ["name"]=>
    string(5) "96774"
    ["posts_per_page"]=>
    int(20)
    ["post_status"]=>
    string(0) ""
    ["perm"]=>
    string(0) ""
  }
  ["query_vars"]=>
  &array(60) {
    ["m"]=>
    string(1) "0"
    ["s"]=>
    string(0) ""
    ["paged"]=>
    int(1)
    ["order"]=>
    string(0) ""
    ["orderby"]=>
    string(0) ""
    ["post_type"]=>
    string(9) "finalists"
    ["events"]=>
    string(5) "96774"
    ["name"]=>
    string(5) "96774"
    ["posts_per_page"]=>
    int(20)
    ["post_status"]=>
    string(0) ""
    ["perm"]=>
    string(0) ""
    ["error"]=>
    string(0) ""
    ["p"]=>
    int(0)
    ["post_parent"]=>
    string(0) ""
    ["subpost"]=>
    string(0) ""
    ["subpost_id"]=>
    string(0) ""
    ["attachment"]=>
    string(0) ""
    ["attachment_id"]=>
    int(0)
    ["pagename"]=>
    string(0) ""
    ["page_id"]=>
    int(0)
    ["second"]=>
    string(0) ""
    ["minute"]=>
    string(0) ""
    ["hour"]=>
    string(0) ""
    ["day"]=>
    int(0)
    ["monthnum"]=>
    int(0)
    ["year"]=>
    int(0)
    ["w"]=>
    int(0)
    ["category_name"]=>
    string(0) ""
    ["tag"]=>
    string(0) ""
    ["cat"]=>
    string(0) ""
    ["tag_id"]=>
    string(0) ""
    ["author"]=>
    string(0) ""
    ["author_name"]=>
    string(0) ""
    ["feed"]=>
    string(0) ""
    ["tb"]=>
    string(0) ""
    ["meta_key"]=>
    string(0) ""
    ["meta_value"]=>
    string(0) ""
    ["preview"]=>
    string(0) ""
    ["sentence"]=>
    string(0) ""
    ["title"]=>
    string(0) ""
    ["fields"]=>
    string(0) ""
    ["menu_order"]=>
    string(0) ""
    ["embed"]=>
    string(0) ""
    ["category__in"]=>
    array(0) {
    }
    ["category__not_in"]=>
    array(0) {
    }
    ["category__and"]=>
    array(0) {
    }
    ["post__in"]=>
    array(0) {
    }
    ["post__not_in"]=>
    array(0) {
    }
    ["post_name__in"]=>
    array(0) {
    }
    ["tag__in"]=>
    array(0) {
    }
    ["tag__not_in"]=>
    array(0) {
    }
    ["tag__and"]=>
    array(0) {
    }
    ["tag_slug__in"]=>
    array(0) {
    }
    ["tag_slug__and"]=>
    array(0) {
    }
    ["post_parent__in"]=>
    array(0) {
    }
    ["post_parent__not_in"]=>
    array(0) {
    }
    ["author__in"]=>
    array(0) {
    }
    ["author__not_in"]=>
    array(0) {
    }
    ["search_columns"]=>
    array(0) {
    }
    ["meta_query"]=>
    array(1) {
      [0]=>
      array(3) {
        ["key"]=>
        string(14) "award_event_id"
        ["compare"]=>
        string(1) "="
        ["value"]=>
        string(5) "96774"
      }
    }
  }
  ["tax_query"]=>
  NULL
  ["meta_query"]=>
  bool(false)
  ["date_query"]=>
  bool(false)
  ["post_count"]=>
  int(0)
  ["current_post"]=>
  int(-1)
  ["before_loop"]=>
  bool(true)
  ["in_the_loop"]=>
  bool(false)
  ["comment_count"]=>
  int(0)
  ["current_comment"]=>
  int(-1)
  ["found_posts"]=>
  int(0)
  ["max_num_pages"]=>
  int(0)
  ["max_num_comment_pages"]=>
  int(0)
  ["is_single"]=>
  bool(true)
  ["is_preview"]=>
  bool(false)
  ["is_page"]=>
  bool(false)
  ["is_archive"]=>
  bool(false)
  ["is_date"]=>
  bool(false)
  ["is_year"]=>
  bool(false)
  ["is_month"]=>
  bool(false)
  ["is_day"]=>
  bool(false)
  ["is_time"]=>
  bool(false)
  ["is_author"]=>
  bool(false)
  ["is_category"]=>
  bool(false)
  ["is_tag"]=>
  bool(false)
  ["is_tax"]=>
  bool(false)
  ["is_search"]=>
  bool(false)
  ["is_feed"]=>
  bool(false)
  ["is_comment_feed"]=>
  bool(false)
  ["is_trackback"]=>
  bool(false)
  ["is_home"]=>
  bool(false)
  ["is_privacy_policy"]=>
  bool(false)
  ["is_404"]=>
  bool(false)
  ["is_embed"]=>
  bool(false)
  ["is_paged"]=>
  bool(false)
  ["is_admin"]=>
  bool(true)
  ["is_attachment"]=>
  bool(false)
  ["is_singular"]=>
  bool(true)
  ["is_robots"]=>
  bool(false)
  ["is_favicon"]=>
  bool(false)
  ["is_posts_page"]=>
  bool(false)
  ["is_post_type_archive"]=>
  bool(false)
  ["query_vars_hash":"WP_Query":private]=>
  string(32) "451749b2be0ba93a642e5efa44f4f745"
  ["query_vars_changed":"WP_Query":private]=>
  bool(false)
  ["thumbnails_cached"]=>
  bool(false)
  ["allow_query_attachment_by_filename":protected]=>
  bool(false)
  ["stopwords":"WP_Query":private]=>
  NULL
  ["compat_fields":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(15) "query_vars_hash"
    [1]=>
    string(18) "query_vars_changed"
  }
  ["compat_methods":"WP_Query":private]=>
  array(2) {
    [0]=>
    string(16) "init_query_flags"
    [1]=>
    string(15) "parse_tax_query"
  }
}

本文标签: wp queryFiltering custom post type list in admin by custom meta keyvalue