If you are using the WordPress as your business website and also using the custom post type. Also, want to include the custom post type in your search to extend your site search functionalities.
Please keep your close attention in this post as I am going to share the how to extend search functionality to include custom post meta. Open your functions.php files from your current activated themes folder and add the below code at the end of the file and save it.
Also checkout:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /* * Extend wp search to include custom post meta */ function custom_search_query( $query ) { if ( !is_admin() && $query->is_search ) { $query->set('meta_query', array( array( 'key' => '__meta_key__', 'value' => $query->query_vars['s'], 'compare' => 'LIKE' ) )); $query->set('post_type', '__your_post_type__'); // optional }; } add_filter( 'pre_get_posts', 'dc_custom_search_query'); |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.
Article Tags: wordpress custom post type, wordpress custom post types, wordpress tutorial, wordpress tutorial for beginner, wordpress tutorial for beginners, wordpress tutorials, wordpress tutorials for beginners, wordpress tutorils