我的 Wordpress 头版既有特色文章(样式在框内),也有近期帖子列表(样式分开)。我想使用 Wordpress 循环显示这些最近的帖子,不包括特色帖子。排除某个类别或标签很容易,但在我的情况下,我想排除带有自定义字段的帖子。特色帖子有一个自定义字段,其名称和值为:特色 = 是。
如何在不使用插件的情况下实现这一点?
我的 Wordpress 头版既有特色文章(样式在框内),也有近期帖子列表(样式分开)。我想使用 Wordpress 循环显示这些最近的帖子,不包括特色帖子。排除某个类别或标签很容易,但在我的情况下,我想排除带有自定义字段的帖子。特色帖子有一个自定义字段,其名称和值为:特色 = 是。
如何在不使用插件的情况下实现这一点?
您可以使用meta_query
参数,如http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters中所述
就像是:
$args = array(
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query( $args );
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured',
'value' => 'yes',
'compare' => '='
),
));
$ids = array();
$query = new WP_Query($args); // fetching posts having featured = yes
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$ids[] = $post->ID; // building array of post ids
}
}
$args = array( 'post__not_in' =>$ids); // excluding featured posts from loop
query_posts($args);
while (have_posts()) : the_post();
// rest of the code