我怎样才能得到这两个自定义后类型“文件夹”和“文件”是在自定义字段相同的自定义字段值的所有帖子“WPCF秘密-ID-1”,并在情况下,如果有结果,我想展示另一个自定义字段 'WPCF秘密-ID-2' 的值。
我曾尝试下面的代码,但似乎不工作:
function get_all_post_from_field_value($postid)
{
$args = array(
'post_type' => array(
'folder', 'file'
),
'meta_query' => array(
array(
'key' => 'wpcf-secret-id-1',
'value' => ( $postid )
)
)
);
// The Query
$query = new WP_Query($args);
// The Loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// I am not sure how to get the field value of 'wpcf-secret-id-2'
return get_post_field('wpcf-secret-id-2');
}
}
}
add_shortcode( 'get-posts-by-field-value', 'get_all_post_from_field_value');
我想在结束时获得的所有帖子,其中第一个自定义字段的值是相同的电流后,并显示结果为第二个自定义字段值的环
看起来它应该工作,我看不出有什么明显的错误与您的查询。
这可能会发生的唯一的事情是:在这些自定义字段隐藏?你有没有在你的post_meta表中查找在数据库中查询正确的密钥要查询wpcf-secret-id-1
和wpcf-secret-id-2
?如果他们是从后台隐蔽,他们可能有前置这样一个下划线:_wpcf-secret-id-1
。
此外,您WP_Query使用心不是在这种情况下是正确的。要覆盖整个WordPress的循环与$query->the_post();
与wp_reset_postdata重置前正在返回该功能的。
一个更好的办法来做到这一点是:
$query = new WP_Query($args);
if ($query->found_posts > 0) {
$post = $query->posts[0];
return get_post_meta($post->ID, 'wpcf-secret-id-2', true);
}
else
{
return ""; //Good habit: if you are returning some value, make sure you always return *something*, even when there is no value to return.
}