我正在从搜索结果页面中的URL解析一个搜索词,如下所示:
http://localhost:8888/domain/?s=Varilite%20Icon%20Mid&post_type=knowledge_hub
我需要search.php模板文件来查询ACF关系自定义字段,以查看链接的文章标题是否与URL中解析的变量匹配,并显示结果。下面是我的代码,但它不起作用,因为它只显示帖子标题中带有搜索词的帖子。如何让我的查询同时检查自定义字段?请记住,这个临时文件也用于默认的全局搜索,因此需要灵活。
<?php
$args = array(
'posts_per_page' => -1,
//'fields' => 'ids',
'post_type' => 'knowledge_hub',
'meta_query' => array(
array(
'key' => 'related_products',
'value' => '"'.get_search_query().'"',
'compare' => 'LIKE',
)
),
);
$relatedProductArticles = get_posts($args);
if ( have_posts($relatedProductArticles) ) :
while ( have_posts($relatedProductArticles) ) : the_post($relatedProductArticles);
?>
<article class="col-12 search-item mb-5">
<div class="row d-flex align-items-center">
<div class="col-md-7">
<?php the_post_thumbnail('medium', ['class' => 'w-100']); ?>
</div>
<div class="col-md-4">
<div class="px-4">
<h2><a class="" href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</article>
<?php
endwhile;
else : ?>
<h2><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' );?></h2>
<?php endif;
?>
使用$_GET
进行TRY。
<?php
$args = array(
'posts_per_page' => -1,
//'fields' => 'ids',
'post_type' => 'knowledge_hub',
'meta_query' => array(
array(
'key' => 'related_products',
'value' => $_GET['s'],
'compare' => 'LIKE',
)
),
);
$relatedProductArticles = get_posts($args);
if ( have_posts($relatedProductArticles) ) :
while ( have_posts($relatedProductArticles) ) : the_post($relatedProductArticles);
?>
<article class="col-12 search-item mb-5">
<div class="row d-flex align-items-center">
<div class="col-md-7">
<?php the_post_thumbnail('medium', ['class' => 'w-100']); ?>
</div>
<div class="col-md-4">
<div class="px-4">
<h2><a class="" href="<?php the_permalink() ?>"><?php the_title() ?></a></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
</article>
<?php
endwhile;
else : ?>
<h2><?php _e( 'Sorry, no posts matched your criteria.', 'textdomain' );?></h2>
<?php endif;
?>