• Resolved david_fair

    (@david_fair)


    I have my blog set up to show one post per page. (Site Admin -> Settings -> Reading) This seems to also control how many results are displayed when you use the Search or Archive features. Is there not a separate way to control the number of results displayed when using Search or Archive? I don’t want my readers searching for a topic and finding only one post in the results when there are actually several blog posts.

    Please advise. Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Open your theme’s functions.php

    Add the following on a new line after the very first <?php tag at the top of the file..

    function search_results_per_page( $query ) {
    	if( $query->is_search )
    		$query->set( 'posts_per_page' , 10 );
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'search_results_per_page' );

    So the top of the file now looks like this..

    <?php
    function search_results_per_page( $query ) {
    	if( $query->is_search )
    		$query->set( 'posts_per_page' , 10 );
    	return $query;
    }
    add_filter( 'pre_get_posts' , 'search_results_per_page' );

    Feel free to adjust 10 to the desired amount per page..

    The function above basically sets the posts per page to 10 (or whatever you want), whenever it’s a search, else it just returns (does nothing)..

    😉

    Thread Starter david_fair

    (@david_fair)

    Excellent! It worked. Thank you. 🙂

    Do you know if there is a similar function I can call to increase the results to 10 (or whatever) when using the Archive drop-down in the sidebar and also when clicking on a tag or category?

    Thanks again for the quick, easy fix for search results!

    Switch statement would be fine, just update the above function..

    function search_results_per_page( $query ) {
    
    	switch( true ) {
    
    		case ( $query->is_search ) :
    			$query->set( 'posts_per_page' , 10 );
    		break;
    
    		case ( $query->is_archive ) :
    			$query->set( 'posts_per_page' , 20 );
    		break;
    
    		case ( $query->is_category ) :
    			$query->set( 'posts_per_page' , 30 );
    		break;
    
    	}
    	return $query;
    }

    ..assuming you want to use differing values..

    Else if they’re going to have the same value, then something like…

    function search_results_per_page( $query ) {
    
    	switch( true ) {
    
    		case ( $query->is_search || $query->is_archive || $query->is_category ) :
    			$query->set( 'posts_per_page' , 10 );
    		break;
    
    	}
    	return $query;
    }

    If you’re doing this for “ALL” pages though, then it would be easier to just set the “Blog pages show at most” setting via Admin > Settings > Reading ..

    Thread Starter david_fair

    (@david_fair)

    It worked! Thank you so much! 🙂

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Search and Archive results are incomplete’ is closed to new replies.