0

I've got a custom loop function in my child theme, in the templates.php file to loop through specific post types ('listings') and then put it into an array to use into a part of my template.

Here's an example of the code

public function loop()
{

    global $wp_query;
    $featured = $regular = array();
    foreach ($wp_query->posts as $count => $post)
        if (has_term('featured', 'listings_tag', $post->ID))
            $featured[] = $post;
        else
            $regular[] = $post;
    $posts = array_merge($regular, $featured);
    $posts = array_reverse($posts);
    

    include('templates/loop.php');

}

So this is great, however, it paginates after every 11 posts. Instead I would like it to display everything instead.

I tried by adding a custom action in my functions.php, making use of the 'pre_get_posts' hook. However it doesn't have any effect on my code.

function show_all($query){
    if (is_post_type_archive('listings') && $query->is_main_query()) {
        $query->set('nopaging', True);
    }
}

add_action('pre_get_posts', 'show_all');

Any suggestions?

5
  • You should be calling the methods on the query object, not the helper function, e.g. $query->is_post_type_archive...... Otherwise, nopaging doesn't do what you think it does. Likewise it looks like you've re-implemented sticky posts
    – Tom J Nowell
    Commented Jan 20, 2021 at 21:14
  • Also note that fetching all posts can be dangerous, it's much safer to fetch a high number of posts you never expect to hit, otherwise accidents, changing business cases, etc can lead to memory exhaustion or intense slowness
    – Tom J Nowell
    Commented Jan 20, 2021 at 21:27
  • @TomJNowell thanks for the warning. It's only for a very specific post type with minimal data, so won't be an issue for most servers to handle. :) Commented Jan 21, 2021 at 7:48
  • @TomJNowell Going to try that code now, thank you! Commented Jan 21, 2021 at 7:49
  • if that's the case then ask for 500 posts, never say you want everything. That way instead of saying it should be okay, you can say with certainty and guarantee that will always be okay
    – Tom J Nowell
    Commented Jan 21, 2021 at 10:52

1 Answer 1

1

nopaging does not fetch all posts, it just tells WordPress not to bother figuring out stuff like how many pages there are.

What you actually wanted was not to disable pagination, that's just a solution to your problem that you asked about. You should ask about your problem instead, aka how to show all posts at once.

Showing all posts at once can be dangerous, so instead, set a limit that's very high, e.g:

$query->set('posts_per_page', 500 );

Anything more than 500 posts will probably cause problems, or be extremely slow, as well as being extremely long winded. The idea being that you specify a number you never expect to reach. That way you know for a fact that the worst case scenario can't happen. Why trust that there won't be a broken plugin or a client who does something by accident when you can know for a fact the page won't break?

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.