0

I've been reading several different articles/posts to see if it could help me with my problem, but no luck so far. My goal is to make a custom search for a custom post type: restaurants. When I'm using the custom search I still get all posts from the site except the ones from my custom post type.

At the moment I'm using Filter & Search plugin to filter between custom categories for the custom post type, which works as it should. When I add search with or without the categories it does not - it simply can't find anything.

My custom post type in functions.php looks like this:

function custom_post_type() {
$labels = array(
    'name'                => _x( 'Restaurants', 'Post Type General Name', 'wp-bootstrap-starter-child' ),
    'singular_name'       => _x( 'Restaurant', 'Post Type Singular Name', 'wp-bootstrap-starter-child' ),
    'menu_name'           => __( 'Restaurants', 'wp-bootstrap-starter-child' ),
    'parent_item_colon'   => __( 'Parent Restaurant', 'wp-bootstrap-starter-child' ),
    'all_items'           => __( 'All Restaurants', 'wp-bootstrap-starter-child' ),
    'view_item'           => __( 'View Restaurant', 'wp-bootstrap-starter-child' ),
    'add_new_item'        => __( 'Add New Restaurant', 'wp-bootstrap-starter-child' ),
    'add_new'             => __( 'Add New', 'wp-bootstrap-starter-child' ),
    'edit_item'           => __( 'Edit Restaurant', 'wp-bootstrap-starter-child' ),
    'update_item'         => __( 'Update Restaurant', 'wp-bootstrap-starter-child' ),
    'search_items'        => __( 'Search Restaurant', 'wp-bootstrap-starter-child' ),
    'not_found'           => __( 'Not Found', 'wp-bootstrap-starter-child' ),
    'not_found_in_trash'  => __( 'Not found in Trash', 'wp-bootstrap-starter-child' ),
);

$args = array(
    'label'               => __( 'restaurants', 'wp-bootstrap-starter-child' ),
    'description'         => __( 'Restaurant news and reviews', 'wp-bootstrap-starter-child' ),
    'labels'              => $labels,
    // Features this CPT supports in Post Editor
    'supports'            => array( 'title', 'thumbnail', 'revisions', 'custom-fields' ),
    // You can associate this CPT with a taxonomy or custom taxonomy. 
    'taxonomies'          => array( 'restaurant-category', 'restaurant-category-placering', 'restaurant-category-pris' ),
    'hierarchical'        => false,
    'public'              => true,
    'show_ui'             => true,
    'show_in_menu'        => true,
    'show_in_nav_menus'   => true,
    'show_in_admin_bar'   => true,
    'menu_position'       => 5,
    'can_export'          => true,
    'has_archive'         => true,
    'exclude_from_search' => false,
    'publicly_queryable'  => true,
    'capability_type'     => 'post',
);
register_post_type( 'restaurants', $args );

} add_action( 'init', 'custom_post_type', 0 );

I've tried several diffent forms for the search submit, all with the <input type="hidden" name="post_type" value="restaurants"> but it still only search regular posts.

I've a custom search.php aswell looking like this:

$args = array(
    'post_type'=> 'restaurants',
            's'    => $s,
            'paged' => $paged,
            );
    query_posts($args);
// Template Name: Search Results
$search_refer = $_GET["post_type"];
if ($search_refer == 'restaurants') { load_template(TEMPLATEPATH . '/search-restaurants.php'); }
else { custom HTML

At this point I'm thinking I'm missing a step, where I include the custom post type in search function or something like that.

PS.: when I type this www.example.com/?post_type=restaurants I'll get the custom archive - not sure if it is relevant.

Forgot to mention, that the site is currently using the theme 15Zine

Thanks in advance!

0

The simplest way to only search a particular custom post type, and use its template for the results, is to set the action URL on the search form for the post type archive URL of your post type. Doing this means that WordPress will automatically limit results to that post type, and it will use the same template as your post type archive:

<form method="get" action="<?php echo esc_url( get_post_type_archive_link( 'restaurants' ) ); ?>">

The resulting URL will be:

https://example.com/restaurants/?s=search+term

Some reasons this might not work are:

  1. Your archive templates improperly use a custom WP_Query or query_posts(). Never use either of these to display the main list of posts in a WordPress template.
  2. You are using pre_get_posts to modify the main query in such a way that would interfere with the default behaviour.
  3. The "Filter & Search" that you're using interferes with the default behaviour in some way. This plugin could be affected by 1 & 2 as well.

If you are married to using the Filter & Search plugin, but want to change the template in some way for your custom post type, then you'll need to consult its documentation/support channels for the proper way to do that. It could be something completely. Just keep in mind point #3 above. Using query_posts in the way you have is almost certainly breaking the plugin.

  • Hey and thanks for your answer, Jacob! My archive have got if(have_posts()) : while(have_posts()) : the_post(); and my custom search.php with the pre_get_posts is now deleted and I'm using your form action, but I still get all the other posts and none from my custom post type. – S. Gski Nov 22 '19 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.