2016-09-03 364 views
0

我的搜索页面结果有问题。我已经创建了搜索主题。 当我搜索单词'موفقیت',它已经在我的数据库中它返回所有结果,并正常工作,但我只是想在特殊的自定义帖子类型中搜索不是所有的帖子和pages.how我可以解决这个问题wordpress搜索结果页面

这里是我的代码

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

任何想法都会被占用。

+0

如果你想通过邮局类型过滤,加['post_type'(https://codex.wordpress.org/wp_query#Type_Parameters)到您的查询。 –

+0

我添加了'$ allsearch =&new WP_Query('s = $ s&showposts = -1','post_type'=>'book'); '但它没有工作。 ( – mkafiyan

+0

当然它不起作用。无论你使用数组语法或查询synax的参数。混合它们不起作用。 –

回答

0

试试这个,

$search_query['post_status'] = 'publish'; 
$search_query['post_type'] = 'book'; //your custom post type 
// Custom query. 
$search = new WP_Query($search_query); 

// Check that we have query results. 
if ($search->have_posts()) { 

    // Start looping over the query results. 
    while ($search->have_posts()) { 

     $search->the_post(); 

     // Contents of the queried post results go here. 

    } 

} 

// Restore original post data. 
wp_reset_postdata(); 
+0

它不起作用,我尝试改变我的代码。 – mkafiyan

+0

尝试通过添加'$ search_query [posts_per_page] ='-1';'在$ search = new WP_Query($ search_query)之前。 ' –

+0

我尝试改变我的编码方式,但它完美地工作,但我只想搜索特殊的自定义帖子类型'book'。你知道我该如何解决它吗? – mkafiyan