Php 从自定义查询WordPress中排除特色文章
这是我运行的自定义查询,用于显示来自2个类别的帖子。 我已经安装了WordPress插件“特色文章”,但特色文章并不排除在显示列表之外Php 从自定义查询WordPress中排除特色文章,php,wordpress,Php,Wordpress,这是我运行的自定义查询,用于显示来自2个类别的帖子。 我已经安装了WordPress插件“特色文章”,但特色文章并不排除在显示列表之外 <?php $category_id = get_cat_ID($strReports || $strInsights); $custom_query = new WP_Query( 'cat=' .$category_id. '&featured=no&posts_per_page=6&order=desc'
<?php
$category_id = get_cat_ID($strReports || $strInsights);
$custom_query = new WP_Query( 'cat=' .$category_id. '&featured=no&posts_per_page=6&order=desc' );
while($custom_query->have_posts()) : $custom_query->the_post();
?>
HTML Content Here
<?php endwhile; ?>
<?php wp_reset_query(); // reset the query ?>
这里的HTML内容
查看“特色帖子”,您可以看到它只是针对yes值进行测试。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);
<?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 ?>
希望有帮助 您的计算机正在上运行无效的参数 应该是这样的:
$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);
<?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 ?>
创建自定义查询并添加筛选器
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' );
你确定featured=no是正确的查询参数吗?我添加了与你给出的相同的代码,但现在没有显示帖子。