0

This was the solution that i got from @Ray Gulick for having a custom search form for custom post type. This works fine in hosting service but fails in localhost as the form action is added as action="/" if i change it to action="/localhostprojectname/" then it works .

Any workaround to fix this?


You can restrict a search to a custom post type by modifying a basic WP search form like this:

<form id="cptsearch" action="/" method="get">
    <input type="text" name="s" />
    <input type="hidden" name="post_type" value="POSTTYPENAME" />
    <input id="searchsubmit" type="submit" alt="Search" value="Search" />
</form>

To select a specialized template for the custom post type search, add this filter in your functions file:

function template_chooser($template) {
    global $wp_query;
    $post_type = get_query_var('post_type');
    if( $wp_query->is_search && $post_type == 'POSTTYPENAME' ) {
    return locate_template('page_POSTTYPENAME.php');
    }
    return $template;
}

add_filter('template_include', 'template_chooser');

And of course, you must create the specialized search results template: page_POSTTYPENAME.php

0

The form has to updated as

 <form id="cptsearch" action="<?php echo home_url(); ?>" method="get">
        <input type="text" name="s" />
        <input type="hidden" name="post_type" value="POSTTYPENAME" />
        <input id="searchsubmit" type="submit" alt="Search" value="Search" />
    </form>
| improve this answer | |

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.