从自定义查询WordPress中排除精选帖子

时间:2016-03-01 12:28:47

标签: php wordpress

这是我正在运行的自定义查询,用于显示来自2个类别的帖子。 我已经安装了WordPress插件“精选帖子”,但精选帖子不会从显示的列表中排除。

the_content();

3 个答案:

答案 0 :(得分:2)

浏览“精选帖子”,您可以看到它只是针对值进行测试。 feature=yes所做的只是检查一个元字段,所以我认为你可以采取相反的方式来实现你想要的东西,如下所示:

$args = array(
    'cat' => $category_id,
    'posts_per_page' => 6,
    'order' => 'DESC', // since order default value is already DESC you can remove this line
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => '_is_featured',
            'value' => 'yes',
            'compare' => '!=',
        ),
        array(
            'key' => '_is_featured',
            'value' => 'foo', // Prior to WP 3.9 we have to provide any non-empty string here
            'compare' => 'NOT EXISTS',
        ),
    ) ,
);
$custom_query = new WP_Query($args);

希望它有所帮助!

答案 1 :(得分:1)

您在get_cat_ID上运行无效参数。

应该是这样的:

<?php 
     $cat_names  = array('Cat_Name1', 'Cat_Name2');
     $category_Name = implode(',', $cat_names);
     $args = array(
        'category_name' =>  $category_Name,
        'posts_per_page' => 6,
        'meta_query' => array(
                array(
                    'key' => 'featured', 
                    'value' => true, 
                    'compare' => '!=',
                ),
            ), 
     );
     $custom_query = new WP_Query( $args );
     while($custom_query->have_posts()) : $custom_query->the_post();
?> 

<?php the_title();?>

<?php endwhile; ?>
<?php wp_reset_query(); // reset the query ?>

答案 2 :(得分:0)

创建自定义查询并添加过滤器。

function SearchFilter($query){

$query=" SELECT Distinct SQL_CALC_FOUND_ROWS p.* FROM `$wpdb->posts` as p left join `$wpdb->postmeta` as m on m.post_id=p.ID "
                ." left join `$wpdb->term_relationships` as r ON (p.ID = r.object_id) "
                ." left join `$wpdb->term_taxonomy` as x ON (x.term_taxonomy_id = r.term_taxonomy_id) "
                ." left join `$wpdb->terms` as t ON (t.term_id = x.term_id AND x.taxonomy='category') "
                ." WHERE p.post_type = 'post' AND p.post_status = 'publish' "
                ." AND t.term_id='$idcategorie' "

return $query;
}
add_filter( 'posts_request', 'SearchFilter' );