分类查询始终使用get_posts()
用于侧查询。query_posts()
仅用于主查询和new WP_Query()
应仅用于第二次查询。Read more here.
您可以按分类法(类别、术语等)查询帖子:
$cat_posts = new WP_Query( array(
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'movie_janner\',
\'field\' => \'slug\',
\'terms\' => array( \'action\', \'commedy\' )
),
array(
\'taxonomy\' => \'actor\',
\'field\' => \'id\',
\'terms\' => array( 103, 115, 206 ),
\'operator\' => \'NOT IN\'
)
)
) );
如果要按快捷码进行查询,还可以在查询内容时添加/删除筛选器。
让舒尔阅读评论carefully!
function wpse49871_shortcode_query_filter( $where )
{
global $wpdb;
// Lets be on the safe side, escape and such.
$new_where = $wpdb->prepare(
"%s AND %s LIKE %s"
,$where
,"{$wpdb->posts}.post_content"
// If you know the exact ID, then just insert it in here.
,like_escape( \'%fileurl=%\' )
);
return $new_where;
}
function wpse49871_get_shortcode_posts()
{
add_filter( \'posts_where\', \'wpse_shortcode_query_filter\' );
$posts = get_posts( array(
// Do your query in here. See "Taxonomy Query" args above for example
) );
// Don\'t need it anymore after this run
remove_filter( \'posts_where\', \'wpse_shortcode_query_filter\' );
return $posts;
}
An
even better approach 可能是将其封装在一个类中,如另一个Q中所示。