get_posts not returning all posts - 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.

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)

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.

Related

Include Sticky Posts with Custom Query Posts

I'm working with Custom Query in WordPress, Basically, I'm showing 4 most recent posts of a category having ID 4
and my query is as follows:-
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 4,
'cat' => '3',
'paged' => get_query_var('paged'),
);
$q = new WP_Query($args);
This is working fine, but here I have an additional requirement. I want to add sticky posts as well i.e Posts will be stick to the top no matters these posts are recent or old, and total posts_per_page should be always 4 including sticky and recent posts.
e.g If there is no sticky post then I'll show 4 most recent posts and no sticky post. But if there is 1 sticky post then there will be 1 sticky post and 3 most recent posts, a total of 4 posts.
What modification should I made in my Query so that pagination also works fine as well? Thank you.
you need to just select sticky post option form your post.
Then automatically work your scenario.
Example:
if you have selected one sticky post and you have set post per page 4 then wp_query will fetch the sticky post first and then show the other 3 posts.
if you have not selected sticky post and you have set post per page 4 then wp_query will fetch the only 4 posts without a sticky post.
It is WordPress default behaviour.
Check this code
$sticky = get_option( 'sticky_posts' );
$args = array(
'post_type' => 'post' ,
'orderby' => 'date' ,
'order' => 'DESC' ,
'posts_per_page' => 4,
'cat' => '3',
'paged' => get_query_var('paged'),
'post__in' => isset( $sticky[0] ) ? $sticky[0] : array(),
'ignore_sticky_posts' => 1,
);
$q = new WP_Query($args);

Get related posts from 2 post types same taxonomy

I don't know if this is already covered here but i can't find it.
I'm working on a wordpress project with 3 post types: Hotels, Events and restaurants.
They share one taxonomy (zone).
Now what I want is when I create and event with Zone 1, at the bottom of the single event page display: Hotels from Zone 1 and Restaurants from Zone 1.
Is this even possible to do?
Thanks
I think you can do something like this
$args = array(
'posts_per_page' => -1, //return all posts
'post_status' => 'publish', //only published posts
'post_type' => array('hotels', 'restaurants'),
'tax_query' => array(
'taxonomy' => 'zone',
'terms' => 'zone 1'
),
);
$posts = get_posts($args)
hope that at least points you in the right direction
have a look at this WP_Query

Wordpress: get all posts of a custom type

I have this strange issue. I want to fetch all posts that are of a custom type, here's my snippet.
$query = new WP_Query(array(
'post_type' => 'custom',
'post_status' => 'publish'
));
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
echo $post_id;
echo "<br>";
}
wp_reset_query();
This only gets me 6 of them, while I have more than 50 records matching that criteria in the database. Can anyone tell me where I have gone wrong?
Many thanks!
'posts_per_page' => -1,
Add this to the WP_QUERY array of arguments and it should return all of the posts of this custom post type.
This get all posts of a custom type using get_posts:
$posts = get_posts([
'post_type' => 'custom',
'post_status' => 'publish',
'numberposts' => -1
// 'order' => 'ASC'
]);
The number of posts to return are set under settings > reading
You can pass the number of posts for your query to return using.
'posts_per_page' => 'number of posts'
You should never use:
'posts_per_page' => -1
It slow and not effective, if you are talking about SQL Query speeds. So it is much better to use some large integer.
This is a performance hazard. What if we have 100,000 posts? This could crash the site. If you are writing a widget, for example, and just want to grab all of a custom post type, determine a reasonable upper limit for your situation.
More details here:
https://10up.github.io/Engineering-Best-Practices/php/#performance
It is advisable to use an integer instead of '-1' For example:
'posts_per_page' => 999999,

Modify WooTabs Popular Posts to work with pageviews rather than comment counts

The woo tabs widget display the popular posts based on comment counts rather than page views. The code from the widget than enables this is
$popular = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'comment_count', 'numberposts' => $posts ) );
foreach($popular as $post) :
setup_postdata($post);
?>
Can anyone help me modify thsi so that it works with page views rather than comment counts???
I had the same issue and after a bit of research came up with the following:
I added this snippet to my functions.php file:
Track post views without a plugin using post meta
Then from step 2 of that link I pasted this code snippet above the widget code:
<?php setPostViews(get_the_ID()); ?>
And finally changed 'orderby' => 'comment_count' to 'orderby' => 'post_views' as below:
$popular = get_posts( array( 'suppress_filters' => false, 'ignore_sticky_posts' => 1, 'orderby' => 'post_views', 'numberposts' => $posts ) );
foreach($popular as $post) : setup_postdata($post);
Hope this helps :)

Changing Wordpress post order with URL string

I am trying to add a dropdown to sort my custom posts.
I have tried the solutions here - http://ak.net84.net/php/filter-dropdown-for-wordpress/ - and here - http://blog.rutwick.com/use-jquery-to-reorder-your-wp-posts-on-the-fly
I can’t get either of these to work and I can’t even get my posts to sort by adding this to the end of my URL - ?orderby=title&order=DESC.
Out of curiosity, I went over to DigWP and tried this - http://digwp.com/category/admin/?orderby=title&order=DESC which worked and sorts the posts by title and in descending order.
So I am wondering why it won’t work on my site? Here is the code that is getting my posts.
<?php $my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'paged' => get_query_var('paged'),
));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
I'm guessing there is something wrong with the query or perhaps wp_query won't allow ordering posts in this way?
Any help appreciated.
If you want to get the query string variable, i.e. ?orderby=title
$my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'orderby' => get_query_var('orderby'), // will return orderby query string variable
'order' => 'DESC',
'paged' => get_query_var('paged'),
));

Resources