我试图在我的主题中显示特色帖子,带有一个自定义字段mytheme_featured_post
这是特色帖子中的第一条。
然而,它似乎并没有将帖子过滤到元查询中的帖子。
// WP_Query arguments.
$featured = array(
\'posts_per_page\' => \'5\',
\'meta_query\' => array(
array(
\'key\' => \'mytheme_featured_post\',
\'value\' => \'1\',
),
),
);
// The Query.
$featured_query = new WP_Query( $featured );
if ( $featured_query -> have_posts() ) {
while ( $featured_query -> have_posts() ) : $featured_query -> the_post();
the_title();
endwhile;
}
Update:此参数按预期工作:
// WP_Query arguments.
$featured = array(
\'posts_per_page\' => \'5\',
\'cat\' => \'1\',
\'meta_key\' => \'mytheme_featured_post\',
\'meta_value\' => \'1\',
);
最合适的回答,由SO网友:Ole Kristian Losvik 整理而成
我被粘帖骗了。如果有人遇到同样的问题,这里是工作代码!排除胶粘物后检查是否有效。
/**
* WP_Query arguments.
*
* @link https://codex.wordpress.org/Class_Reference/WP_Query
*/
$args = array(
\'posts_per_page\' => \'4\',
\'orderby\' => \'modified\',
\'order\' => \'desc\',
\'post__not_in\' => get_option( \'sticky_posts\' ),
\'meta_query\' => array(
array(
\'key\' => \'mytheme_featured_post\',
\'value\' => \'1\',
),
),
);
// The Query.
$query = new WP_Query( $args );
// The Loop.
if ( $query->have_posts() ) {
while ( $query->have_posts() ) :
$query->the_post();
the_title();
endwhile;
}
wp_reset_postdata();