Php WP_查询中的自定义字段值
我需要一个WP_查询,它显示所有与显示文章具有相同自定义字段值的文章 这是我的代码:Php WP_查询中的自定义字段值,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我需要一个WP_查询,它显示所有与显示文章具有相同自定义字段值的文章 这是我的代码: function show_other_posts() { //Get the current custom field value if( get_field('desktop_cat') ){ $redirect_value = the_field('desktop_cat'); //Echo the current
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\u value,
,因为当我手动输入一个值时,它工作得很好。这个变量一定有问题
有什么想法吗
非常感谢您字段()会回显字段值。您应该改为使用(返回,而不是回显字段值):
也许尝试转义这个值?
$redirect_value = get_field('desktop_cat');