Published on 06/28/2023
Published by amit
Supporting "Page" in global search for WordPress
Put the following code in the functions.php file or site-specific plugin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
//this code is written for special support for including page in search results for global search only. function manipulate_search_query(WP_Query $query) { if (!$query->is_search()) { return $query; } if (is_admin()) { return $query; } if(isset($_REQUEST['post_type']) && in_array('page', $_REQUEST['post_type'])){ // Set the Post Types that should be searched for the keyword. $query->set('post_type', array('as_events','as_announcements','as_news', 'page', 'post')); } if(isset($_REQUEST['orderby']) && in_array('page', $_REQUEST['orderby'])){ // Set the Post Types that should be searched for the keyword. $query->set('orderby',$_REQUEST['orderby']); } return $query; } /* * Filter: pre_get_posts */ add_filter('pre_get_posts', 'manipulate_search_query', 10, 1); |
Note: Remember to replace the post_type array in the above code with the correct post types that you want to search