使用自定义字段排除Wordpress上的特定Post ID



所以我有多个自定义的帖子类型。在一个特定的自定义帖子类型(a)上,我希望引入另一种类型的帖子(b)。但我在(a)上有一个自定义字段,它是需要排除的(b)帖子之一的ID。

(a)上的这个自定义字段对每个帖子都会更改,因此不能硬编码。

到目前为止,我有这个:

<?php
unset($args);
$exclude = the_field( "exclude_tipster_id" );
$args = array('exclude' => $exclude, 'post_type' => 'oneoff', 'posts_per_page' => 1, 'order' => 'rand');
$lastposts = get_posts($args);
?>
<?php foreach ($lastposts as $post) : setup_postdata($post); ?>

但它什么也没做。我可能在做傻事。你看不出我是一个程序员,所以我用的是谷歌和我的一点点常识。

谢谢你的帮助!

$exclude = the_field( "exclude_tipster_id" );中,应该使用get_field()而不是the_field()

当您使用the_field()时,该值将回显到屏幕上,当您使用get_field()时,它将分配给$exclude。

有关更多信息,请参阅此处的文档:https://www.advancedcustomfields.com/resources/get_field/https://www.advancedcustomfields.com/resources/the_field/