WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Search and Archive results are incomplete (5 posts)

  1. david_fair
    Member
    Posted 4 years ago #

    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.

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    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)..

    ;)

  3. david_fair
    Member
    Posted 4 years ago #

    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!

  4. Mark / t31os
    Moderator
    Posted 4 years ago #

    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 ..

  5. david_fair
    Member
    Posted 4 years ago #

    It worked! Thank you so much! :)

Topic Closed

This topic has been closed to new replies.

About this Topic