Php 如何查询自定义关系字段并将解析的搜索词包含到循环中,并在WordPress中显示结果

Php 如何查询自定义关系字段并将解析的搜索词包含到循环中,并在WordPress中显示结果,php,wordpress,search,relationship,advanced-custom-fields,Php,Wordpress,Search,Relationship,Advanced Custom Fields,我正在解析搜索结果页面中URL中的搜索词,如下所示: http://localhost:8888/domain/?s=Varilite%20Icon%20Mid&post_type=knowledge_hub 我需要search.php模板文件来查询ACF关系自定义字段,以查看链接的文章标题是否与URL中解析的变量匹配,并显示结果。下面是我的代码,但它不工作,因为它只显示文章标题中带有搜索词的文章。如何让查询同时检查自定义字段?请记住,这个tempalte文件也用于默认的全局搜索,因此

我正在解析搜索结果页面中URL中的搜索词,如下所示:

http://localhost:8888/domain/?s=Varilite%20Icon%20Mid&post_type=knowledge_hub
我需要search.php模板文件来查询ACF关系自定义字段,以查看链接的文章标题是否与URL中解析的变量匹配,并显示结果。下面是我的代码,但它不工作,因为它只显示文章标题中带有搜索词的文章。如何让查询同时检查自定义字段?请记住,这个tempalte文件也用于默认的全局搜索,因此需要灵活

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

尝试使用
$\u GET

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


默认情况下,get\u search\u查询是转义的,请尝试
'value'=>get\u search\u查询(false)
抱歉,这没有任何影响,我可以看到我发现
$\u get['s']
将术语回送到数组中,但是由于某些原因,查询没有获取自定义字段
相关的\u产品
。很抱歉,这不起作用。啊,你说得对,
$\u GET['s']
将术语回送到数组中,但是由于某些原因,查询没有获取自定义字段
相关的\u产品