• Hi all,

    I have a custom site that I’m developing (my first, using a custom theme I’m developing specifically for it), and I need some custom functionality that Google can’t help me with. The site in question has several sections, but there are two we’re focusing on: ‘Work’ and ‘Blog’.

    The blog section features just what it says, a blog, with a search field and category filtering – each blog post is a ‘post’.

    The work page features work done previously by this company, and it ALSO features a search field and category filtering. Each ‘work’ is a ‘page’.

    Here’s the thing: I need these two searches to be completely separate. Searching on the ‘blog’ page should only return blog posts, and searching on the ‘work’ page should only return work. Is there any way to have the two pages call two completely different search results? I know I can exclude pages or exclude posts, but I’ve only been able to do that globally (by putting it in the ‘search.php’ file), not dynamically based on WHERE the user searched from.

    In fact, the layout of the search page itself also changes completely based on where the user searched from – the ‘blog’ search looks completely different from the ‘work’ search. It’s almost as if I need two completely different search.php pages.

    Any assistance at all would be appreciated!

Viewing 1 replies (of 1 total)
  • There are a few possible ways to go about this. I would really need to know more about how your pages are set up before I could offer the best solution.

    My first instinct was to post this snippet of code, which allows you to define a custom post type when searching: http://example.com/?s=pizza&post_type=page

    function wp6728693_pre_get_posts( $query ) {
    	$post_type = $_REQUEST['post_type'];
    
    	if ( is_admin() || ! is_search() || ! $query->is_main_query() || empty( $post_type ) || ! post_type_exists( $post_type ) ) {
    		return $query;
    	}
    
    	$query->query_vars['post_type'] = $post_type;
    
    	return $query;
    }
    
    add_filter( 'pre_get_posts', 'wp6728693_pre_get_posts' );

    However, after giving it more thought, ?s= should already do this when queried on a specific URL, ie: http://example.com/work/?s=pizza

    Another thought that came to mind is using a plugin like Custom Post Type UI to define a “work” post type. This would also require additional code to display all work listings in the work section. Please note that I have never used this plugin personally, so I cannot guarantee that it would achieve the results that I have in mind.

    Would you mind sharing the URL so that I can see the format first-hand?

Viewing 1 replies (of 1 total)
  • The topic ‘Separated Search/Categories – one for Pages, another for Posts?’ is closed to new replies.