Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numberposts, showposts, posts_per_page. What's the difference?

Tags:

wordpress

Every time I see different answer.

Can anyone give me a good reliable answer on the difference between numberposts, showposts, posts_per_page when querying posts?

like image 488
Avishay28 Avatar asked Jan 17 '16 10:01

Avishay28


1 Answers

They are all the same and does exactly the same thing. Usage are slightly different though

  • numberposts, posts_per_page and showposts are all valid in get_posts(). numberposts is set to posts_per_page before being passed to WP_Query. Check the source code

    if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
        $r['posts_per_page'] = $r['numberposts'];
    
  • posts_per_page and showposts are all valid in WP_Query but numberposts not. If showposts is used, it is set to posts_per_page in the WP_Query class. See the source code

    if ( isset($q['showposts']) && $q['showposts'] ) {
        $q['showposts'] = (int) $q['showposts'];
        $q['posts_per_page'] = $q['showposts'];
    }
    

Although the codex says showposts is depreciated, there is still no depreciation notice in core.It was however replaced in favor of posts_per_page.

To answer your question, posts_per_page is probably the best to use as numberposts and showposts are being set to posts_per_page in the WP_Query class

like image 172
Pieter Goosen Avatar answered Oct 14 '22 14:10

Pieter Goosen