今天和同事在使用 WP_Query 的 post__in 參數(shù)的時(shí)候:
$like_query = new WP_Query(array(
'post_type' => array('post','event'),
'post__in' => array(138,139),
'orderby' => 'post__in',
'posts_per_page'=> -1
) );
但是返回的結(jié)果總是超過(guò)這個(gè) 138, 139 這兩篇,甚是奇怪。后面仔細(xì)查看文檔,才發(fā)現(xiàn)有如下這段話:
ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts.
原來(lái)如此,我又正好使用了 sticky posts,置頂文章,所以,哎,調(diào)試了整整好幾個(gè)小時(shí),都把 WP 源代碼翻爛了。
所以最終的代碼應(yīng)該是:
$like_query = new WP_Query(array(
'post_type' => array('post','event'),
'post__in' => array(138,139),
'orderby' => 'post__in',
'posts_per_page' => -1,
'ignore_sticky_posts' => 1
) );
標(biāo)簽:WordPress 技巧