我有一个自定义帖子类型(Books),它与第二个自定义帖子类型(People)相连接。图书CPT有一个额外的作者元框,如果选中,它应该出现在额外的人物CPT上。
然而,即使检查并指定了第二位作者,这本书CPT也不会出现在第二个人CPT的页面上。我的页面代码如下:
<?php
$author_id = get_the_ID();
$single_books_query = new WP_Query(array(
\'post_type\' => \'post\',
\'category_name\' => \'books\',
\'meta_query\' => array(
array(
\'key\' => \'ecpt_pub_author\',
\'value\' => $author_id,
\'type\' => \'NUMERIC\',
\'compare\' => \'=\'
),
\'posts_per_page\' => \'-1\'
)));
if ( $single_books_query->have_posts() ) : while ($single_books_query->have_posts()) : $single_books_query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php if ( has_post_thumbnail()) { the_post_thumbnail(\'directory\', array(\'class\' => "floatleft")); } ?>
<h5><?php the_title();?></h5>
<h6><?php if ( get_post_meta($post->ID, \'ecpt_pub_date\', true) ) : echo get_post_meta($post->ID, \'ecpt_pub_date\', true); endif; ?><?php if ( get_post_meta($post->ID, \'ecpt_publisher\', true) ) :?>, <?php echo get_post_meta($post->ID, \'ecpt_publisher\', true); endif; ?></h6>
<p><b>Role: <span style="text-transform:capitalize;"><?php echo get_post_meta($post->ID, \'ecpt_pub_role\', true); ?></span>
//This is the line that isn\'t working:
<?php if (get_post_meta($post->ID, \'ecpt_author_cond\', true) == \'on\') { $faculty_post_id2 = get_post_meta($post->ID, \'ecpt_pub_author2\', true); ?><br>
<?php echo get_the_title($faculty_post_id2); ?>, <?php echo get_post_meta($post->ID, \'ecpt_pub_role2\', true); ?>
<?php } ?>
</b></p>
</a>
<hr>
<?php endwhile; endif; ?>
是否需要建立另一个循环来查询
ecpt_pub_author2
?
UPDATE
我在查询中添加了一个关系键,如果选中,它将显示第二本关于适当人员的书。但如果未选中条件,它也默认为第一个人CPT。为什么会这样?我的更新查询:
$author_id = get_the_ID();
$single_books_query = new WP_Query(array(
\'post_type\' => \'post\',
\'category_name\' => \'books\',
\'meta_query\' => array(
\'relation\'=> \'AND\',
array(
\'key\' => \'ecpt_pub_author\',
\'value\' => $author_id,
\'type\' => \'NUMERIC\',
\'compare\' => \'=\'
),
array(
\'key\'=>\'ecpt_pub_author2\',
\'value\' => $author_id,
\'type\' => \'NUMERIC\',
\'compare\' => \'=\'
),
\'posts_per_page\' => \'-1\'
)));