post type - How to restrict wordpress search to pages and disable products listing search?

I have a requirement where i need to restrict wordpress search to pages listing only and disable products listing . In o

I have a requirement where i need to restrict wordpress search to pages listing only and disable products listing . In other words i wanted to replace post type in the search url from 'products' to 'page'. Any possible solution ?

I have a requirement where i need to restrict wordpress search to pages listing only and disable products listing . In other words i wanted to replace post type in the search url from 'products' to 'page'. Any possible solution ?

Share Improve this question asked Aug 2, 2019 at 5:33 Syam SuthanSyam Suthan 111 bronze badge 1
  • It was solved as i uninstalled woo commerce plugin. – Syam Suthan Commented Aug 2, 2019 at 5:56
Add a comment  | 

1 Answer 1

Reset to default 1

There are better ways, but one easy way to achieve this if you wanted to customise the search results is to edit the search.php template and make the WP_Query a custom one:

<?php
$args = array(
    's' => get_search_query(),
    'post_type' => array('page'),
);

$search_query = new WP_Query($args);

if ( have_posts() ) :

    while ( $search_query->have_posts() ) : $search_query->the_post();
        // do stuff here
    endwhile;

    wp_reset_query();

endif;
?>

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信