Create Second Search Page Only for Custom Post Type

Question

I created a custom post type that will not be displayed anywhere on the site. However, I want to access the data from a search form. Because there will be a lot of pages, I need to disable the post type from showing up in the normal search and create a second search page for only searching the keyword in custom post types.

I’ve read quite a few pages and people give different advice and sometimes say to avoid certain functions, so I’m not sure what is the most updated way to do this.

I started by cutting and pasting my “search.php” into 2 partial templates and then changing “search.php” to

<?php
if(isset($_GET['search-type'])) {
    $type = $_GET['search-type'];
    if($type == 'custom-post-type') {
        get_template_part('/partials/search-custom-post-type');

    } else{
        get_template_part('/partials/search-normal');
    }
}
else{
        get_template_part('/partials/search-normal');
    }
?>

I read that I could add the following code and it would allow me to customize the search-custom-post-type.php

$args = array( 'post_type' => 'custom-post-type-name' );
$args = array_merge( $args, $wp_query->query );
query_posts( $args );

However, this doesn’t do anything. The search is still the same.

0
jonbon 7 months 2020-12-20T23:10:46-05:00 0 Answers 5 views 0

Leave an answer

Browse
Browse