在管理页面上按自定义帖子类型中的自定义字段过滤


11

我已经使用“高级自定义字段”为比赛名称,答案等创建了自定义字段。如图所示,我为比赛创建了自定义帖子类型,并使用Wordpress functions.php从我的自定义字段值中创建了列。

我正在尝试使用一个具有不同名称/标签的竞赛的下拉列表框,如下所示,但是我只能使用分类法来查找解决方案,如果可能的话,我宁愿不使用它,因为我只使用了自定义字段其他一切。

是否可以仅使用自定义字段来创建自定义“过滤依据”下拉菜单?

Wordpress筛选依据


您可以使用restrict_manage_posts动作挂钩添加其他下拉框。但是不要忘记,您还必须为过滤器添加一些逻辑,因为WP不知道如何使用它(不同于分类法下拉列表,它可以自动处理)。
大卫·加德

还有其他想法-如果您愿意,可以在列表表中链接“ 名称”,这意味着您可以通过单击名称而不是下拉菜单来过滤比赛。
大卫·加德

Answers:


12

为了显示过滤器的结果,请尝试以下代码

add_filter( 'parse_query', 'prefix_parse_filter' );
function  prefix_parse_filter($query) {
   global $pagenow;
   $current_page = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';

   if ( is_admin() && 
     'competition' == $current_page &&
     'edit.php' == $pagenow && 
      isset( $_GET['competition-name'] ) && 
      $_GET['competition-name'] != '') {

    $competion_name = $_GET['competition-name'];
    $query->query_vars['meta_key'] = 'competition_name';
    $query->query_vars['meta_value'] = $competition_name;
    $query->query_vars['meta_compare'] = '=';
  }
}

根据需要更改元键和元值。我将“竞争名称”作为meta_key,将“竞争名称”作为select下拉名称。


很好,我感到很懒,因此建议他再问一个问题;)
David Gard 2015年

似乎这2个答案是一个完整的答案,应结合使用。
RCNeil

10

restrict_manage_posts动作触发add_extra_tablenav()功能,这是你如何添加额外的下拉菜单到您想要的清单表。

在下面的示例中,我们首先确保Post Type是正确的,然后我们对照表中的competition_name键获取所有存储的DB值postmeta(您必须根据需要更改键名)。该查询是相当基本的查询,仅检查是否已发布竞赛,仅采用唯一值(您不想在下拉菜单中重复),然后按字母顺序对其进行排序。

接下来,我们检查结果(没有点输出下拉菜单,什么也不做),然后构造选项(包括显示所有内容的默认值)。最后,输出下拉列表。

如我的评论所述,这还不是故事的结局。您将需要一些逻辑来告诉“列表”仅在过滤器处于活动状态时显示所需的结果,但是我将留给您看一下,然后在需要进一步帮助时提出另一个问题。提示 -签出文件/wp-admin/includes/class-wp-posts-list-table.php,它是父文件.../wp-class-list-table.php

/**
 * Add extra dropdowns to the List Tables
 *
 * @param required string $post_type    The Post Type that is being displayed
 */
add_action('restrict_manage_posts', 'add_extra_tablenav');
function add_extra_tablenav($post_type){

    global $wpdb;

    /** Ensure this is the correct Post Type*/
    if($post_type !== 'competition')
        return;

    /** Grab the results from the DB */
    $query = $wpdb->prepare('
        SELECT DISTINCT pm.meta_value FROM %1$s pm
        LEFT JOIN %2$s p ON p.ID = pm.post_id
        WHERE pm.meta_key = "%3$s" 
        AND p.post_status = "%4$s" 
        AND p.post_type = "%5$s"
        ORDER BY "%3$s"',
        $wpdb->postmeta,
        $wpdb->posts,
        'competition_name', // Your meta key - change as required
        'publish',          // Post status - change as required
        $post_type
    );
    $results = $wpdb->get_col($query);

    /** Ensure there are options to show */
    if(empty($results))
        return;

    // get selected option if there is one selected
    if (isset( $_GET['competition-name'] ) && $_GET['competition-name'] != '') {
        $selectedName = $_GET['competition-name'];
    } else {
        $selectedName = -1;
    }

    /** Grab all of the options that should be shown */
    $options[] = sprintf('<option value="-1">%1$s</option>', __('All Competitions', 'your-text-domain'));
    foreach($results as $result) :
        if ($result == $selectedName) {
            $options[] = sprintf('<option value="%1$s" selected>%2$s</option>', esc_attr($result), $result);
        } else {
            $options[] = sprintf('<option value="%1$s">%2$s</option>', esc_attr($result), $result);
        }
    endforeach;

    /** Output the dropdown menu */
    echo '<select class="" id="competition-name" name="competition-name">';
    echo join("\n", $options);
    echo '</select>';

}

使用此方法时,我得到了错误Notice: wpdb::prepare was called incorrectly. The query does not contain the correct number of placeholders (6) for the number of arguments passed (5). Please see Debugging in WordPress for more information. (This message was added in version 4.8.3.) in /[...]/wp-includes/functions.php on line 4773
rassoh

我也遇到了同样的错误
Vasim Shaikh,

0

如果这不适用于任何人,则我的解决方案是将我尝试过滤的列添加到自定义帖子类型的可排序列列表中。

// Make Custom Post Type Custom Columns Sortable
function cpt_custom_columns_sortable( $columns ) {

    // Add our columns to $columns array
    $columns['item_number'] = 'item_number';
    $columns['coat_school'] = 'coat_school'; 

    return $columns;
} add_filter( 'manage_edit-your-custom-post-type-slug_sortable_columns', 'cpt_custom_columns_sortable' );

0

替换下面的查询以更正wpdb:prepare错误:

$query = $wpdb->prepare('
        SELECT DISTINCT pm.meta_value FROM %1$s pm
        LEFT JOIN %2$s p ON p.ID = pm.post_id
        WHERE pm.meta_key = "%3$s" 
        AND p.post_status = "%4$s" 
        AND p.post_type = "%5$s"
        ORDER BY "%3$s"',
        $wpdb->postmeta,
        $wpdb->posts,
        'competition_name', // Your meta key - change as required
        'publish',          // Post status - change as required
        $post_type,
        'competition_name' //this is needed a second time to define "%3$s" in ORDER BY
  );
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.