[Code] WP_theme_search : adding post types

Kakao Share

// File Path : wp-content/themes/generatepress/custom-function.php

//search result sort by post_types
function custom_search_filter($query) {
    if ($query->is_search && !is_admin()) {
        $query->set('post_type', array('post', 'page', 'kboard')); // Replace 'post' and 'page' with your desired post types
        $query->set('orderby', 'modified'); // Order by modified date
        $query->set('order', 'DESC'); // Sort in descending order
		$query->set('posts_per_page', 1000); // desired number of serach results per page
    }
    return $query;
}
add_filter('pre_get_posts', 'custom_search_filter');

//get_post_types() from local server debugging, where no elementor installation enviroment. 
	post: "post"
	page: "page"
	attachment: "attachment"
	revision: "revision"
	nav_menu_item: "nav_menu_item"
	custom_css: "custom_css"
	customize_changeset: "customize_changeset"
	oembed_cache: "oembed_cache"
	user_request: "user_request"
	wp_block: "wp_block"
	wp_template: "wp_template"
	wp_template_part: "wp_template_part"
	wp_global_styles: "wp_global_styles"
	wp_navigation: "wp_navigation"
	kboard: "kboard"
	sc_event: "sc_event"
	=> Adding $query->set('post_type', array('post', 'page', 'kboard', 'sc_event')

		}
	}
}
add_action('kboard_document_insert', 'my_reply_email_notify', 10, 2); //

//get post_type operation server by console log
	["post"]=> string(4) "post" => post
	["page"]=> string(4) "page" => page
	["attachment"]=> string(10) "attachment" 
	["revision"]=> string(8) "revision" 
	["nav_menu_item"]=> string(13) "nav_menu_item" 
	["custom_css"]=> string(10) "custom_css" 
	["customize_changeset"]=> string(19) "customize_changeset" 
	["oembed_cache"]=> string(12) "oembed_cache" 
	["user_request"]=> string(12) "user_request" 
	["wp_block"]=> string(8) "wp_block" 
	["wp_template"]=> string(11) "wp_template" 
	["wp_template_part"]=> string(16) "wp_template_part" 
	["wp_global_styles"]=> string(16) "wp_global_styles" 
	["wp_navigation"]=> string(13) "wp_navigation" 
	["wp_font_family"]=> string(14) "wp_font_family" 
	["wp_font_face"]=> string(12) "wp_font_face" 
	["wpcode"]=> string(6) "wpcode" 
	["e-landing-page"]=> string(14) "e-landing-page" 
	["elementor_library"]=> string(17) "elementor_library" 
	["wpdiscuz_form"]=> string(13) "wpdiscuz_form" 
	["kboard"]=> string(6) "kboard" => kboard
	["m-chart"]=> string(7) "m-chart" => chart
	["mpl_chapter"]=> string(11) "mpl_chapter" 
	["rs_elements"]=> string(11) "rs_elements" => timeline
	["sc_event"]=> string(8) "sc_event" => calendar
	["pmb_project"]=> string(11) "pmb_project" 
	["pmb_design"]=> string(10) "pmb_design" 
	["pmb_content"]=> string(11) "pmb_content" 

//final application code : search result sort by post_types
function custom_search_filter($query) {
    if ($query->is_search && !is_admin()) {
    	// Replace 'post' and 'page' with your desired post types
        $query->set('post_type', 
                    array('post', 'page', 'kboard', 'sc_event', 'rs_elements', 'm-chart')); 
        $query->set('orderby', 'modified'); // Order by modified date
	}
      return $query;
}
add_filter('pre_get_posts', 'custom_search_filter');

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

카카오톡 채널 친구추가
0
Would love your thoughts, please comment.x
()
x