在创建cpt的自定义搜索时需要帮助

时间:2019-05-09 08:33:53

标签: php wordpress

搜索应仅显示相关的分类法帖子,但要占用整个cpt帖子

我正在从头开始创建一个工作板,我已经创建了一种自定义职位类型,称为工作,创建了分类法作为工作位置,在functions.php中定义了函数,并创建了一个搜索页面,如下所示:

第一步:创建名为作业的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' );

第二步:创建分类法

//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>

第4步:创建了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-->

第5步:创建位置过滤器为

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

问题:

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

2 个答案:

答案 0 :(得分:1)

如果要按用户过滤,应将job_location保存在用户meta中,然后在tax_query中保存如下:

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

答案 1 :(得分:0)

在此处了解更多信息:什么时候应该使用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();