Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get_posts not returning all posts

Tags:

wordpress

I have to mount the blog posts manually, but I'm not sure if this is the correct way to work, It only brings 9 pages, with 4 posts each, but the blog has 83 posts!

<?php     $paged = get_query_var('paged');     $args = array(         'numberposts'       => 4,         'offset'            => $paged*4,         'orderby'           => 'post_date',         'order'             => 'DESC',         'post_type'         => 'post',         'post_status'       => 'publish',         'suppress_filters'  => true     );     $posts_array = get_posts( $args ); ?> 

Thanks anyway.

like image 316
Rafael Moni Avatar asked Aug 17 '12 17:08

Rafael Moni


People also ask

How do I get all my posts on WordPress?

You just need to define an array of parameters and pass it to the get_posts function. WordPress converts that array into a real and secure MySQL query, runs it against the database, and returns an array of posts. Second, you have to traverse the result set returned by get_posts with a foreach cycle.

How do I find category posts in WordPress?

Now, if you want to display all your posts from a specific category on a separate page, WordPress already takes care of this for you. To find the category page, you simply need to go to Posts » Categories » View page and click on the 'View' link below a category.

How do I fetch a blog post on WordPress?

You could use the WP_query class, the get_posts function or the query_posts function (it's important to note right in the beginning that the query_posts function comes with a warning). Put very simply, the main purpose of any of these is to retrieve a set of posts. But the method used by each is different.


2 Answers

Problem is your 'numberposts' is set to 4 Put it at -1 to get all posts:

 'numberposts'       => -1, 

If you don't set numberposts here, WordPress will pull the number of posts from your Dashboard settings (under Settings -> Reading)

like image 177
AJ Zane Avatar answered Sep 29 '22 01:09

AJ Zane


The below note is from this codex section.

Note: With use of the offset, the above query should be used only on a category that has more than one post in it, otherwise there'll be no output.

So in-order to display all posts, there should be at-least 2 posts in each categories.

You can try Loops to get all posts. Check The Loop in Action also.

like image 35
Libin Avatar answered Sep 29 '22 01:09

Libin