簡體   English   中英

如何查詢帖子自定義關系字段並將解析的搜索詞包含在循環中並在 WordPress 中顯示結果

[英]How do I query posts custom relationship field and include parsed search term into loop and display results in WordPress

我在搜索結果頁面中解析來自 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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM