在Admin Posts页面中添加快速链接,我可以在其中按meta_key进行查询

时间:2016-11-14 作者:danyeah

我有一个自定义字段is-active 使用高级自定义字段(ACF)创建。我想在所有帖子发布后添加一个快速链接,该链接过滤所有具有以下值的帖子true 对于is-active 元键。

我使用此功能成功地将活动链接添加到列表:

add_filter(\'views_edit-ground_catalog\', \'ground_add_admin_quick_link\');
function ground_add_admin_quick_link($views) {

    if( ( is_admin() ) && ( $_GET[\'post_type\'] == \'ground_catalog\' ) ) {

     global $wp_query;
     unset($views[\'mine\']);
     $args =  array(
        \'post_type\' => \'ground_catalog\',
        \'orderby\' => \'date\',
        \'order\' => \'DESC\',
        \'posts_per_page\' => 5,
        \'meta_key\'      => \'is-active\',
        \'meta_value\'    => true,
        );


     $result = new WP_Query($args);

     $views[\'publish_f\'] = sprintf(\'<a href="%s">Attivi<span class="count">(%d)</span></a>\', admin_url(\'edit.php?post_status=publish&post_type=ground_catalog&active=true\'), $result->found_posts);

     return $views;

 }
查询变量的代码:

  function register_active() {
      global $wp;
      $wp->add_query_var( \'active\' );
  }
  add_action( \'init\', \'register_active\' );

  function map_active( $wp_query ) {
      if ( $meta_value = $wp_query->get( \'active\' ) ) {
        $wp_query->set( \'meta_key\', \'is-active\' );
        $wp_query->set( \'meta_value\', $meta_value );
      }
  }
add_action( \'parse_query\', \'map_active\' );
检查查询时,所有变量都设置正确

Query\'s variables:
active  true
meta_key    is-active
meta_value  true
但是,过滤器没有显示任何帖子。我错过了什么?

我只需要一个链接edit.php?post_status=publish&post_type=ground_catalog&meta_key=is-active&meta_value=true

1 个回复
最合适的回答,由SO网友:Dave Romsey 整理而成

在创建了一个名为ground_catalog 支持自定义字段,然后创建一些测试帖子。我添加了自定义字段is-active (使用标准WordPress功能)并输入值true 对于一些帖子。

修复方法是将参数改为WP_Query:

$result = new WP_Query( [
    \'post_type\'  => $post_type,
    \'orderby\'    => \'date\',
    \'order\'      => \'DESC\',
    \'meta_key\'   => \'is-active\',
    \'meta_value\' => \'true\', // Note that this is a STRING
] );
完整示例下面是完整的工作代码示例,其中包含一些可读性重构,以及当前过滤器项样式的修复和其他小更改:

add_filter( \'views_edit-ground_catalog\', \'wpse246143_add_admin_quick_link\' );
function wpse246143_add_admin_quick_link( $views ) {
    $post_type = \'ground_catalog\';

    // Bail immediately if we\'re not looking at the right post type.
    if ( ! isset( $_GET[\'post_type\'] ) || $post_type !== $_GET[\'post_type\'] ) {
        return $views;
    }

    // Handle the class applied to the link filter.
    $link_class = false;
    if ( isset( $_GET[\'active\'] ) && \'true\' === $_GET[\'active\'] ) {
        $link_class = \'current\';
    }

    // Set up a query to determine how many posts are set up as is-active == true
    $result = new WP_Query( [
        \'post_type\'  => $post_type,
        \'orderby\'    => \'date\',
        \'order\'      => \'DESC\',
        \'meta_key\'   => \'is-active\',
        \'meta_value\' => \'true\',
    ] );

    // Generate the link for our filter. 
    // post_status=publish was removed to prevent a styling conflict with the existing \'Published\' filter.
    $views[\'ground_catalog_active\'] = sprintf( \'<a href="%s" class="%s">%s<span class="count">(%d)</span></a>\',
            admin_url( "edit.php?post_type={$post_type}&active=true" ),
            $link_class,
            __( \'Attivi\', \'text-domain\' ),
            $result->found_posts
    );

    return $views;
}

add_action( \'init\', \'wpse246143_register_active\' );
function wpse246143_register_active() {
    global $wp;
    $wp->add_query_var( \'active\' );
}

add_action( \'parse_query\', \'wpse246143_map_active\' );
function wpse246143_map_active( $wp_query ) {
    $meta_value = $wp_query->get( \'active\' );

    if ( true == $meta_value ) {
        $wp_query->set( \'meta_key\', \'is-active\' );
        $wp_query->set( \'meta_value\', $meta_value );
    }
}
为了完整性和故障排除,下面是注册ground_catalog post类型以及is-active 元列。

// Register ground_catalog post type
add_action( \'init\', \'wpse246143_register_ground_catalog\' );
function wpse246143_register_ground_catalog() {
    $labels = array(
        "name" => __( \'Ground Catalogs\', \'your_text_domain\' ),
        "singular_name" => __( \'Ground Catalog\', \'your_text_domain\' ),
        );

    $args = array(
        "label" => __( \'Ground Catalogs\', \'your_text_domain\' ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => false,
        "rest_base" => "",
        "has_archive" => true,
        "show_in_menu" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "ground_catalog", "with_front" => true ),
        "query_var" => true,

        "supports" => array( "title", "editor", "thumbnail", "custom-fields" ),                 );

    register_post_type( "ground_catalog", $args );
}

// Add custom column to ground_catalog post list
add_filter( \'manage_ground_catalog_posts_columns\' , \'wpse246143_manage_ground_catalog_columns\' );
function wpse246143_manage_ground_catalog_columns( $columns ) {
    return array_merge( $columns, 
        array( \'is-active\' => __( \'Is Active\', \'your_text_domain\' ) ) );
}

// Display custom column values on ground_catalog post list
add_action( \'manage_ground_catalog_posts_custom_column\' , \'wpse246143_ground_catalog_custom_column\', 10, 2 );
function wpse246143_ground_catalog_custom_column( $column, $post_id ) {
    switch ( $column ) {
        case \'is-active\':
            $active = get_post_meta( $post_id, \'is-active\', true );
            echo \'<strong>is-active: </strong>\';
            var_dump( $active );
            break;
    }
}
演示我添加了一些ground_post 用于故障排除和测试目的的条目。自定义列显示为is-active 元值。将显示在Active (Attivi)排序列是仅含污垢的地面和含树叶的地面,因为它们是唯一具有is-active 元键设置值为true (字符串):

ground_post list

正在排序

enter image description here