0

I have a site with a subsection containing a custom post type, "artwork"

I have an overall site search working how I'd like it to - it searches all of my posts and custom post types. I would like to add a second search inside of my artwork subsection, that only searches the "artwork" custom post type. I know I can limit my search results like this:

function mySearchFilter($query) {

    if ($query->is_search) {
            $query->set('post_type','artwork');
    };
    return $query;
};

add_filter('pre_get_posts','mySearchFilter');

But that affects the sitewide search too. How can I set the post type ONLY on certain instances of the search form? I don't need a dropdown or any way for the user to choose, there are just certain pages where I'd like to include limited search instead of the overall site search.

  • How do you determine which instances the code should effect? I am not asking for the code, just the logic. Certain pages? Specific templates? What? – s_ha_dum Apr 30 '15 at 18:29
  • the single-artwork.php page and the archive-artwork.php page - my problem is that I'm not sure how to a conditional to a pre_get_posts filter based on where the query request is coming from (single-artwork.php), as opposed to the template it is affecting (search.php) - or if that's even possible? – elemenopy Apr 30 '15 at 18:35
0

If you have a "natural" artwork archive (typically setting has_archive to true in your register_post_type() arguments), you already have the functionality in place:

http://example.com/artwork/?s=query

...assuming artwork is your archive slug, WordPress will search only artwork for "query".

Just set the action attribute of the search form to the archive URL, like so:

<form action="<?php echo get_post_type_archive_link( 'artwork' ) ?>">
  • Ah! Perfect - so simple I wasn't thinking along those lines at all. Thank you..!! – elemenopy Apr 30 '15 at 18:37

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.