wp query - Searching in specific custom post type

I have a problem with my search page result. I already created a search theme.When I search for a word like 'موفقی

I have a problem with my search page result. I already created a search theme. When I search for a word like 'موفقیت' (which it is already in my database), it returns all results and works fine. But I just want to search in specific custom post type not all posts and pages.

Here is my code:

    <?php 
 get_header(); ?>
<div class="title-pack col-md-12 col-sm-12 col-xs-12">
        <span class="line visible-sm-block"></span>
        <span class="visible-sm-block tittle-style">نتایج جستجوی شما</span>
    </div>
<div id="search-results" class="wrapper" role="search">


<!-- COUNT RESULTS -->
<div class="results">
    <?php
    /* Search Count */
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1);
    $count = $allsearch->post_count; 
    _e('');
    _e('"<span class="search-terms">');
    echo $key;
    _e('</span>"'); 
    echo $count . ' مورد یافت شد';
    wp_reset_query(); ?>

</div>
<!-- / COUNT RESULTS -->

<?php if ($allsearch->have_posts()) : ?>
<?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?>

<!-- LIST RESULTS -->
<section>   
    <h3>
        <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
        <?php the_title_attribute(); ?>"><?php the_title(); ?></a> - 
        <span class="search-time"><?php the_time('F, j, Y') ?></span>
        </li>
    </h3>
</section>
<!-- / LIST RESULTS -->

<?php endwhile; else: ?>

<!-- 404 SEARCH -->
<div class="404-search">
<?php _e("Oops... We couldn't find what you were searching for. Please try again"); ?>
</div>
<!-- / 404 SEARCH -->

<?php endif; ?>

     </div>


 <div style="clear:both;"></div>            
 <?php get_footer(); ?> 


     <div style="clear:both;"></div>            
     <?php get_footer(); ?> 

Any ideaa would be appropriated.

I have a problem with my search page result. I already created a search theme. When I search for a word like 'موفقیت' (which it is already in my database), it returns all results and works fine. But I just want to search in specific custom post type not all posts and pages.

Here is my code:

    <?php 
 get_header(); ?>
<div class="title-pack col-md-12 col-sm-12 col-xs-12">
        <span class="line visible-sm-block"></span>
        <span class="visible-sm-block tittle-style">نتایج جستجوی شما</span>
    </div>
<div id="search-results" class="wrapper" role="search">


<!-- COUNT RESULTS -->
<div class="results">
    <?php
    /* Search Count */
    $allsearch = &new WP_Query("s=$s&showposts=-1"); 
    $key = wp_specialchars($s, 1);
    $count = $allsearch->post_count; 
    _e('');
    _e('"<span class="search-terms">');
    echo $key;
    _e('</span>"'); 
    echo $count . ' مورد یافت شد';
    wp_reset_query(); ?>

</div>
<!-- / COUNT RESULTS -->

<?php if ($allsearch->have_posts()) : ?>
<?php while ($allsearch->have_posts()) : $allsearch->the_post(); ?>

<!-- LIST RESULTS -->
<section>   
    <h3>
        <li>
        <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to 
        <?php the_title_attribute(); ?>"><?php the_title(); ?></a> - 
        <span class="search-time"><?php the_time('F, j, Y') ?></span>
        </li>
    </h3>
</section>
<!-- / LIST RESULTS -->

<?php endwhile; else: ?>

<!-- 404 SEARCH -->
<div class="404-search">
<?php _e("Oops... We couldn't find what you were searching for. Please try again"); ?>
</div>
<!-- / 404 SEARCH -->

<?php endif; ?>

     </div>


 <div style="clear:both;"></div>            
 <?php get_footer(); ?> 


     <div style="clear:both;"></div>            
     <?php get_footer(); ?> 

Any ideaa would be appropriated.

Share Improve this question edited Apr 8, 2019 at 20:41 butlerblog 5,1313 gold badges28 silver badges44 bronze badges asked Sep 3, 2016 at 9:55 mkafiyanmkafiyan 1513 silver badges11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default -1

Include post_type parameter in your search query.

Please see this link for more detailed examples

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745613665a4636102.html

相关推荐

  • wp query - Searching in specific custom post type

    I have a problem with my search page result. I already created a search theme.When I search for a word like 'موفقی

    10小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信