【问题标题】:need help in creating custom search for cpt在为 cpt 创建自定义搜索时需要帮助
【发布时间】:2019-05-09 08:33:53
【问题描述】:

搜索应该只显示相关的分类帖子,但它会占用整个 cpt 帖子

我正在从头开始创建一个工作板,我创建了一个名为工作的自定义帖子类型,创建了分类作为工作位置,在 functions.php 中定义了函数,创建了一个搜索页面,如下所示:

step1:创建名为作业的 CPT

//added cpt
function codex_custom_init() {

register_post_type(
        'Jobs', array(
          'labels' => array('name' => __( 'Jobs' ), 'singular_name' => __( 'Jobs' ) ),
          'public' => true,
          'show_ui' => true,
          'capability_type' => 'post', 
          'hierarchical' => false, 
          'rewrite' => true,
          'has_archive' => 'jobs',
          'supports' => array('title', 'editor', 'thumbnail'),
              'menu_icon' => 'dashicons-megaphone',
          /*'taxonomies' => array( 'category' )*/
        )
      );
       }
    add_action( 'init', 'codex_custom_init' );

第 2 步:创建分类法

//taxonomy as joblocation
add_action( 'init', 'create_my_taxonomies', 0 );
    function create_my_taxonomies() {
    register_taxonomy(
        'joblocation',
        'jobs',
        array(
            'labels' => array(
                'name' => 'Job Location',
                'add_new_item' => 'Add New Job Location',
                'new_item_name' => "New Job Location"
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true
        )
    );
}

Step3:我在page.php上的表单

<form role="search" method="get" id="searchform" action="<?php echo home_url( '/' ); ?>">  
   <span class="screen-reader-text"></span>
   <input type="text" name="s" placeholder="Search Job Openings" id="search">
   <input type="submit" id="searchsubmit" value="Search">
   <input type="hidden" name="taxonomy" id="taxonomy" value="joblocation">
</form>

Step4:创建search.php

<!--query starts-->
            <?php if ( have_posts() && strlen( trim(get_search_query()) ) != 0 ) : ?>
             <h1>Search Results for &nbsp;<small><span class="search-for"><?php echo get_search_query(); ?></span></small></h1>
                <?php while ( have_posts() ) : the_post(); ?>
                    <?php if ( has_post_thumbnail() ) : ?>

                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'medium' ); ?></a>

                    <?php else : ?>

                    <?php endif; ?>
                        <a href="<?php echo get_permalink($post->ID) ?>">
                            <div class="open-positions">
                                <h2><?php the_title()?></h2>
                                <div><strong>Key Skills:</strong> <?php the_field('key_skills')?></div>
                                <div><strong>Location:</strong> <?php the_field('location')?></div>
                                <div><strong>Years of Experience:</strong> <?php the_field('years_of_experience')?></div>
                            </div>
                        </a>
                    <?php the_excerpt(); ?>


                <?php endwhile; ?>
                <br/><br/>
                 <a href="javascript:history.back()" id="goback">&#8656; &nbsp;Go Back</a>
            <?php else : ?>

                <h1>No results were found that match your search criteria</h1>
                <br/><br/>
                <div class="no-results-search"><a href="javascript:history.back()" id="goback">&#8656; &nbsp;Go Back</a></div>
            <?php endif ;?>
        <!--query ends-->

Step5:创建位置过滤器

<?php
   $args = array(
               'taxonomy' => 'joblocation',
               'orderby' => 'name',
               'order'   => 'ASC'
           );

   $cats = get_categories($args);

   foreach($cats as $cat) {
?>
      <a href="<?php echo get_category_link( $cat->term_id ) ?>">
           <?php echo $cat->name; ?>
      </a>
<?php
   }
?>

问题:

如果用户位于新泽西州(分类位置),则搜索后用户也可以在搜索结果中看到纽约职位发布。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    如果您想按用户过滤,您应该将 job_location 保存在用户元中,然后按如下方式保存 tax_query:

    $tax_query = array( 'relation' => 'AND', array( 'taxonomy' => 'joblocation', 'terms' => '1', 'field' => 'term_id', ), array( 'taxonomy' => 'joblocation', 'terms' => '2', 'field' => 'term_id', ), );

    【讨论】:

      【解决方案2】:

      在此处了解更多信息:何时应使用 WP_Query、query_posts() 和 get_posts()。

      您必须使用 WP_Query 来获取所需的帖子。阅读它的文档。在您的情况下,查询可能是这样的:

            $the_query = new WP_Query( array(
              'post_type' => 'Jobs',
              'tax_query' => array(
                  array (
                      'taxonomy' => 'joblocation',
                      'field' => 'slug',
                      'terms' => 'NewJersy',
                  )
              ),
      ));
      
      
              while ( $the_query->have_posts() ) :
                 $the_query->the_post();
                  // Show Posts ...
              endwhile;
      /* Restore original Post Data 
       * NB: Because we are using new WP_Query we aren't stomping on the 
       * original $wp_query and it does not need to be reset.
      */
      wp_reset_postdata();
      

      【讨论】:

      • 因为我只有一页可以将所有工作地点显示为 taxonomy-joblocation.php,所以 ('terms' => 'NewJersy') 将如何适用于所有地点??
      猜你喜欢
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2020-09-02
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多