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

标签 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(); ?>

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

谢谢。

最佳答案

好的,所以我终于得到了我想要的东西,但我不确定为什么下面的代码有效而我的原始代码没有,因为它们都是执行新查询的类似方法。

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

<?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; ?>

关于wordpress - WP 查询循环通过在主页上工作但不在搜索页面上的自定义帖子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22655828/

相关文章:

php - 在 Woocommerce 单一产品页面的简短描述下显示自定义字段

php - 在 WooCommerce 电子邮件订单项目中显示可变产品的产品自定义字段

php - WooCommerce:管理员中的动态自定义结帐字段

php - Jquery Slider 有时会出现在奇怪的位置

css - font-face css 自定义字体问题

bash - 在 awk 中使用 while 循环打印列

Java 方法 while 循环无休止,即使似乎满足了突破情况

php - 无法显示 Font-Awesome 图标

php - Wordpress-按自定义字段获取帖子

python - 我的 'lowest common multiple' 程序挂起,没有输出答案