在admin中按acf字段过滤自定义帖子时出错

时间:2018-01-15 07:22:43

标签: wordpress advanced-custom-fields

我正在通过acf字段'issue_year'为自定义帖子'杂志'添加过滤器。我创建下拉列表并过滤帖子,但我收到了以下错误。

错误:

WordPress database error Not unique table/alias: 'wp_postmeta' for query SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id  WHERE 1=1  AND ( 
  ( wp_postmeta.meta_key = 'issue_year' AND wp_postmeta.meta_value = '2017' )
) AND wp_posts.post_type = 'magazine' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'future' OR wp_posts.post_status = 'draft' OR wp_posts.post_status = 'pending' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 20 made by WP_List_Table->display, WP_List_Table->display_tablenav, WP_Posts_List_Table->extra_tablenav, do_action('manage_posts_extra_tablenav'), WP_Hook->do_action, WP_Hook->apply_filters, WPSEO_Link_Columns->count_objects, WPSEO_Link_Columns->set_count_objects, WP_Query->get_posts

代码: 下拉:

add_action('restrict_manage_posts', 'filter_experts_by_issue_year');
function filter_experts_by_issue_year() {
    $post_type = 'magazine';
    global $wpdb;
    $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,
        'issue_year',
        'publish',
        $post_type
    );
    $results = $wpdb->get_col($query);
    if(empty($results))
        return;

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

    /** Output the dropdown menu */
    echo '<select class="" id="issue_year" name="issue_year">';
    echo join("\n", $options);
    echo '</select>';
}
过滤器

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

    if ( is_admin() && 'magazine' == $current_page && 'edit.php' == $pagenow && isset( $_GET['issue_year'] ) && $_GET['issue_year'] != '-1') {
        $query->query_vars['meta_key'] = 'issue_year';
        $query->query_vars['meta_value'] = $_GET['issue_year'];
        $query->query_vars['compare'] = '=';

    }
}

wp版本:4.9.1 acf pro:5.6.5

2 个答案:

答案 0 :(得分:1)

在我看来,你的order by子句有错误

使用此代码

$query = $wpdb->prepare('
    SELECT DISTINCT pm.meta_value 
    FROM $wpdb->postmeta pm
    LEFT JOIN $wpdb->posts p
    ON p.ID = pm.post_id
    WHERE pm.meta_key = "%s" 
    AND p.post_status = "%s" 
    AND p.post_type = "%s"
    ORDER BY pm.meta_value',
    'issue_year',
    'publish',
     $post_type
);

答案 1 :(得分:0)

我得到了错误导致的答案。对于前端搜索,我添加了以下3个过滤器。

add_filter('posts_join', 'cf_search_join' );
add_filter( 'posts_where', 'cf_search_where' );
add_filter( 'posts_distinct', 'cf_search_distinct' );

添加此过滤器导致错误。所以,我把它们改成如下。

if(!is_admin()) {
    add_filter('posts_join', 'cf_search_join' );
    add_filter( 'posts_where', 'cf_search_where' );
    add_filter( 'posts_distinct', 'cf_search_distinct' );
}

希望这有帮助。