WP查询循环通过在主页上工作但不在搜索页面上的自定义帖子类型

NJT*_*NJT 4 wordpress while-loop custom-fields advanced-custom-fields

我有两种自定义的帖子类型,叫做艺术家绘画.两种帖子类型都有一个名为艺术家名称的自定义字段,使用高级自定义字段插件创建.我需要能够将这两种帖子类型中的自定义字段相互匹配,以便显示更多信息.

仅在查询下方粘贴args和循环.如果有必要,将发布更多代码.

<?php
$artist_name = get_field('artist');

$args = array(
'post_type' => 'artists',
'meta_value' => $artist_name
);
$query_artist = new WP_Query( $args );

if ( $query_artist->have_posts() ) {
    while ( $query_artist->have_posts() ) {
        $query_artist->the_post(); ?>
        <p class="artist-name"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
        <?php }
} else {
    echo 'Artist not found';
}
wp_reset_postdata(); ?>
Run Code Online (Sandbox Code Playgroud)

此代码在主页的模板文件中正常工作,但在搜索结果页面中始终打印出"未找到艺术家".我直接从主页模板复制了这段代码,因此拼写错误不是问题.很长一段时间以来,我一直在打破这个局面.读这篇文章的人是否会知道发生了什么?

谢谢.

NJT*_*NJT 5

好吧,所以我设法最终得到了我想要的工作但是我不确定为什么下面的代码工作而我原来没有,因为它们都是类似的方法来做一个新的查询.

如果其他人遇到同样的问题,这是代码:

<?php
// Permalink for artist
$artist_name = get_field('artist');
global $post;
$posts = get_posts( array( 'post_type' => 'artists', 'meta_value' => $artist_name ) );
if( $posts ):
   foreach( $posts as $post ) :   
    setup_postdata($post); ?>
    <p class="artist-name"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
   <?php endforeach; 
wp_reset_postdata(); 
endif; ?>
Run Code Online (Sandbox Code Playgroud)