• Hi all,
    I would like to use the same default search bar (in the header) to make a global site search, but that limits the search to custom post type when in a specific section marked by a slug (eg /books/ )

    But it seems to complicated for me.

    I tried several solutions, (eg post-type-sensitive tool bar) but none of them seem to work. However, a post-type-sensitive tool bar would only work in post, and not in other pages of the section (eg static pages, search results or category pages…). So, i thought that the only way to get a working toolbar for a specific section is to make it “slug-sensitive”

    I found this function to get the slug:

    function the_slug() {
        $post_data = get_post($post->ID, ARRAY_A);
        $slug = $post_data['post_name'];
        return $slug;
    }

    (is it correct?)

    but I don’t know how to continue modifying the search bar to make it “slug-sensitive” activating anything like this:

    <input type="hidden" name="post_type" value="my-custom-post-type" />
    <input type="text" class="field" name="s" id="s" placeholder="<?php esc_attr_e( 'Search for books', 'mytheme' ); ?>" />

    only when slug is equal to /mysection/

    in other words, I would get this result:

    IF slug = /books/ then:

    1. search result limited to custom PT “books”
    2. destination of results is a custom template (eg “books-search.php”)
    3. lastly (but important for usability)the placeholder should be “search for books” (instead “search this site”)

    ELSE search bar works normally

    I know it might seem unnecessarily complicated, but I think it would be an optimal solution for a clean design without additional search bars.
    Unfortunately, after many many attempts, I realized that my knowledge of the codex is insufficient to achieve this.

    So, someone can help me?
    Many many thanks.

    My theme is “pinboard” by Onedesign (I use child theme) and my site is here:
    laputa.it

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘slug-sensitive search bar (it seems too complicated)’ is closed to new replies.