1

I'm trying to override the number of posts to show on pagination for my search.php template, to be 16 vs 4. In the Reading settings on the back end, it is set to 4, which is desired because my front page uses the same code to display the 4 most recent posts.

Is there a way to designate this on the search.php template page?

This is my template page for Search.php

<?php 
    get_header();
    global $wp_query;
?>

<picture class="featured-image block  pos-rel">
    <div class="t-con flex row pos-abs">
        <h1  class="post-title f-center" title="<?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: <?php the_search_query(); ?>"><?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"</h1> <!--Edit these values to adjust page being shown.-->
    </div>
    <?php echo  get_the_post_thumbnail($page=2, 'full', array( 
        'class' => 'featured'
    )); ?>
</picture>

<section class="main bg-darkpurple">
<div>
    <div class="grid row posts">
        <?php while ( have_posts() ) : the_post(); ?>
        <div class="col">
            <div class="date-container">
                <p class="post-date bg-darkpurple">
                    <?php 
                        $u_time = get_the_time('U');
                        $u_modified_time = get_the_modified_time('U');
                            if ($u_modified_time >= $u_time + 86400) {
                        echo "Last updated ";
                            the_modified_time('F jS, Y');
                        echo ""; }
                        else {echo "Posted "; the_time('F jS, Y');}
                    ?>
                </p>
            </div>
            <?php
                $primary_cat = get_post_meta($post->ID , '_category_permalink', true);
                    if ( $primary_cat != null ) {
                        $cat_id = $primary_cat['category'];
                        $category = get_category($cat_id);
                    } else {
                        $categories = get_the_category();
                        $category = $categories[0];
                    }
                        $category_link = get_category_link($category);
                        $category_name = $category->name;  
                ?>

                <picture class="post-thumb block">

                    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('medium'); ?></a>
                    <div class="cat-container">
                        <a class="post-cat bg-darkpurple" href="<?php echo $category_link ?>"><?php echo $category_name ?></a>
                    </div>

                </picture>

                <h3>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_title(); ?>
                    </a>
                </h3>
                    <p><?php echo substr(get_the_excerpt(), 0,160); ?></p>
        </div>
        <?php endwhile; ?>
    </div>
        <div class="pagination flex row f-center">
            <?php 
                echo paginate_links( array(
                    'end_size'     => 2,
                    'mid_size'     => 1,
                    'prev_text'    => sprintf( '<i></i> %1$s', __( 'Other results', 'text-domain' ) ),
                    'next_text'    => sprintf( '%1$s <i></i>', __( 'Other results', 'text-domain' ) ),
                ) );
            ?>
        </div>
    <?php wp_reset_postdata(); ?>
</div>
</section>

<?php get_footer(); ?>

I know this line of code should be what I need, but I'm not sure WHERE to put it:

'posts_per_page' => 16

Thanks for any help!

1
  • 1
    Note that there is no reason to call wp_reset_postdata in that code snippet, you should remove it
    – Tom J Nowell
    Commented Feb 1, 2020 at 0:22

2 Answers 2

1

Perhaps this function will help you out.
Add to functions.php after you made a backup of this file.
There is no need to add any in a template by using this function.

/**
 * Set max output results for search page
 * 
 * @param  $queryVars
 * @return results
 *
 * Read more   @link   https://developer.wordpress.org/reference/classes/requests   /request/
 * Tested with @version 5.3.2
 * 
 */
add_filter( 'request', 'search_results_pro_page' );
function search_results_pro_page( $queryVars )
{
    // Prevent access to back-end
    if ( is_admin() ) {
        return;
    }

    // Set only on search
    if ( isset( $_REQUEST['s'] ) ) {        
        // Adjust number of results shown
        $queryVars['posts_per_page'] = 4;  
    }
    // Return amount search results 
    return $queryVars;                      
}
2
  • Actually, It affects back end somehow. The pages section is broken with this code, and shows the posts instead, Warning: array_keys() expects parameter 1 to be array, null given in C:\xampp\htdocs\streamershaven\wp-includes\class-wp.php on line 530
    – Devon
    Commented Feb 2, 2020 at 3:27
  • Commenting out this code results in the bug going away.
    – Devon
    Commented Feb 2, 2020 at 3:29
1

If you want to modify the posts that WordPress fetches from the database, use the pre_get_posts filter to intercept the query before it goes to the database

e.g.

add_action( 'pre_get_posts', function( WP_Query $query ) {
    // don't mess with admin search, just the frontend
    if ( is_admin() {
        return;
    }
    // if it's a search query, and it's the main query
    if ( $query->is_main_query() && $query->is_search() ) {
        // then change query variables using the set method
        $query->set( '....', '....' );
        // etc..
    }
} );

As it's a hook it can go in functions.php or a plugin. By the time the template is loaded, the query has already fetched the posts, so you would not put it in search.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.