Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Divide search results so it shows posts and than pages

  • I have different types of content in posts and different in pages. (Currently pages are excluded from search results because I don’t want to mix these two) In search results, I would like to list the posts first, just like here https://getyourideal.com/?s=domain and then underneath (below the numeration of posts search results) I would like to add a title like “Results in resources’ and list in a similar fashion (or different) only page results, not posts. Is that possible? How can I do that?

  • I started messing about with the search.php file and it kind of works but not sure what will happen after update

  • Hi there,

    In that case, you can use a child theme. Simply copy the modified search.php file from the parent theme into the child theme directory.

    Here’s a child theme download link:
    https://generatepress.com/api/themes/generatepress_child.zip

  • Assuming I would not touch the search.php file, is there a better way to do this?

  • Hi there,

    You can add 2 query blocks into the loop template element of the search results.

    1. Set post type to post for 1 query block, and set post type to pages for the other query block, do NOT toggle the Inherit query from template option for either of the query block.

    2. Add CSS class search-posts to the 1st query block, and add CSS class search-pages to the 2nd query block.
    Adding CSS class(es): https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/

    3. Add this PHP code.

    add_filter('generateblocks_query_wp_query_args', function ($query_args, $attributes) {
    	// Only run modifications on the search results page
    	if (! is_search()) {
    		return $query_args;
    	}
    
    	// Get the main search query term
    	$search_term = get_search_query();
    
    	// If there's no search term, don't modify (or could force no results)
    	if (empty($search_term)) {
    		// Optional: Force no results if search term is empty for these blocks
    		// $query_args['post__in'] = array(0);
    		return $query_args;
    	}
    
    	// Get the CSS classes assigned to the Query block
    	$block_classes = $attributes['className'] ?? '';
    
    	// Check if this Query block has one of our target classes
    	$is_target_search_loop = (
    		strpos($block_classes, 'search_posts') !== false ||
    		strpos($block_classes, 'search_pages') !== false ||// Add any other classes you use
    		// Add more || conditions for other post type loops
    	);
    
    	// If it's one of our targeted search loops, add the search term parameter
    	if ($is_target_search_loop) {
    		$query_args['s'] = $search_term;
    
    	}
    
    	return $query_args;
    }, 10, 2);

    Adding PHP: https://docs.generatepress.com/article/adding-php/

  • You blow my mind.

    At first I was confused but I know your support is top notch so I followed it and made it happen, but I came across two issues that I think might be hard to overcome.

    1. Pagination is gone from the filtered search results, and 2. Pages show up is search results that are not supposed to, ie. are marked with Exlcude from search.

    As an alternative to all this, is it be possible to add search box or feature that would only show pages in search results? but obviously not the ones that are excluded manually

    thanks for your help

  • As an alternative to all this, is it be possible to add search box or feature that would only show pages in search results?

    Yes, you can use WP’s pre_get_post() filter to only show pages in the search result.

    add_action( 'pre_get_posts', function( $query ) {
        // Only modify the main search query on the frontend
        if ( $query->is_search() && $query->is_main_query() && !is_admin() ) {
            $query->set( 'post_type', 'page' ); 
        }
    });

    1. Pagination is gone from the filtered search results, and

    I see, this would be complicated, the 2 queries are independent of each other, so it’s impossible to have only 1 pagination for both queries, unfortunately.

    2. Pages show up is search results that are not supposed to, ie. are marked with Exlcude from search.

    Did you exluded pages from the query block settings? if so, we can try modify the code to merge the search query args into the original uqery args that GB already built.

    add_filter('generateblocks_query_wp_query_args', function ($query_args, $attributes) {
        // Only run modifications on the search results page
        if ( ! is_search() ) {
            return $query_args;
        }
    
        $search_term = get_search_query();
        if ( empty($search_term) ) {
            return $query_args;
        }
    
        $block_classes = $attributes['className'] ?? '';
    
        $is_target_search_loop = (
            strpos($block_classes, 'search_posts') !== false ||
            strpos($block_classes, 'search_pages') !== false
        );
    
        if ( $is_target_search_loop ) {
            // Merge additional args with the original query args
            $query_args = array_merge(
                $query_args,
                [
                    's'         => $search_term,
                ]
            );
        }
    
        return $query_args;
    }, 10, 2);
    
  • thank you for your help

  • You are welcome   🙂

  • @Ying: I love this solution. It works like a charme!

    Just two small additions:

    1. The first PHP code throws an error when included in functions.php. The solution from July 31, 2025 at 8:29 am, however, works perfectly.
    2. According to the code, the classes should be written with an underscore, not a hyphen, as indicated in the text.

    I also added productsas third posttype, and after a few adjustments, everything worked perfectly. I was already afraid I would have to write a search.php template. 😀

  • @Maik,

    Glad it works for you 🙂

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.