[英]Custom field value inside WP_Query
我想要一個WP_Query,它顯示所有與顯示帖子具有相同自定義字段值的帖子。
這是我的代碼:
function show_other_posts() {
//Get the current custom field value
if( get_field('desktop_cat') ){
$redirect_value = the_field('desktop_cat');
//Echo the current custom field value for debugging
echo $redirect_value;
//Query Posts with same value
$redirect_args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'desktop_cat',
'value' => $redirect_value,
'compare' => '='
)
)
);
//Display the Post Titles
$the_query = new WP_Query ( $redirect_args );
if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;endif;
wp_reset_query();
};
};
問題必須是'value' => $redirect_value,
因為當我手動輸入一個值時,它會很好地工作。 該變量一定有問題。
有任何想法嗎?
非常感謝
the_field()
回顯字段值。 您應該改用get_field()
(它返回而不是回顯字段值):
$redirect_value = get_field('desktop_cat');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.