Limit posts per page after using array_merge in Wordpress - php

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?

Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

Related

Wordpress WP_Query $args, show published posts AND the current user's pending posts

On my Wordpress website users can write their own posts on the group wall. I want to display all published posts on the group wall. I also want to display pending posts to the current user who is the author of these posts. Right now when the user submits a new post, there is no information displayed about it, they may not know if their post got submitted. I want to show them their pending post with the information that they need to wait for the post review.
Is there a way to add the logic to the $args of the WP_Query? This is my code right now:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => 'publish',
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
And later I have my template. This way it is only showing published posts as of now. I tried to find some information about it here: https://developer.wordpress.org/reference/classes/wp_query/ , and later I tried changing the $args array to something like this:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array (
'relation' => 'AND',
array(
'post_status' => 'publish',
),
array(
'post_status' => 'pending',
'author' => $uid,
),
),
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
It is most probably not the way to do this, and obviously, it does not work the way I want. It shows both published and pending posts to all users (not just the current user who is an author). I could maybe create a new WP_Query, but this one is already paginated and showing 5 posts per page with a "Load More" button, I don't want to break the pagination.
Is there a way to create this logic using $args array? Or any other way to achieve what I want?
Thank you in advance.
If there is more information needed or more of the code, I will try to update it.
Try below code.
//number of posts per page default
$num = 5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array(
'key' => 'pm_accessible_groups',
'value' => sprintf(':"%s";',1),
'compare' => 'like'
);
For all publish post.
$publish_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'publish' ),
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$publish_posts = new WP_Query( $publish_args );
//check
if ( $publish_posts->have_posts() ):
//loop
while ($publish_posts->have_posts()): $publish_posts->the_post();
endwhile;
wp_reset_postdata();
endif;
For all pending posts of current user.
$pending_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'pending' ),
'post_author' => $uid,
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$pending_posts = new WP_Query( $pending_args );
//check
if ( $pending_posts->have_posts() ):
//loop
while ($pending_posts->have_posts()): $pending_posts->the_post();
endwhile;
wp_reset_postdata();
endif;

Combine two (post) parameters in a single WordPress query

Is there a way to setup a custom WordPress query to get specific post IDs plus children of other specific posts IDs in the same query? I've tried using post_parent__in and post__in parameters but they stop working when combined.
I think this query would help you to achieve your functionality.. Try this query to get all those posts...
$parent = 2; //change as desired or get all parent post ids
$child_args = array(
'post_type' => 'post',
'post_parent' => $parent
);
$keys = array($parent);
$ids = array_merge($keys, array_keys( get_children( $child_args ) ));
$query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post__in' => $ids,
'posts_per_page' => -1
)
);

Woocommerce - Guys I'm trying to display 3 random products but skipping the first 3 most recent added products

I need your help. I'm trying to display 3 random products but skipping the first 3 most recently added products. Most recent meaning not by query but by global date the product was created.
Heres the code i use to display random products.
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
}
Adding 'offset' only skips the first 3 random. Is there a way to skip first 3 most recently added products?
First, get three last products and get their IDs using wp_get_recent_posts function and map IDs, then add post__not_in argument to WP_query with these three post IDs
$recent_posts = wp_get_recent_posts([
'post_type' => 'product',
'numberposts' => 3
]);
$last_three_posts = array_map(function($a) { return $a['ID']; }, $recent_posts);
$args = array(
'post_type' => 'product',
'orderby' => 'rand',
'posts_per_page' => 3,
'post__not_in' => $last_three_posts,
);
$loop = new WP_Query( $args );

Fill up array with pages wordpress

I have a page for products on my website and I have an array to store all of the pages that have products as a parent. This works fine but for some reason, it stops filling up the array after 10 entries. This is my code to fill up the array.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any');
// The Query
$parent = new WP_Query( $args );
$n = 0;
// The Loop
if ( $parent->have_posts() ) {
echo '<ul>';
while ( $parent->have_posts() ) {
$parent->the_post();
echo '<li>' . get_the_title() . '</li>';
$n++;
echo $n;
}
echo '</ul>';
// Restore original Post Data
wp_reset_postdata();
}
This returns the titles of the Pages and echo's a number to count them. The output shows the titles but stops after 10 pages, even though I have 12 pages with products as a parent. The strange thing is that when I add another page with products as a parent it pushes the last one in the list out and puts the newly added one in the first spot in the array.
Any help is welcome!
Use posts_per_page instead of numberposts.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'posts_per_page' => -1,
'post_status' => 'any');
You can define how many posts are returned by setting posts_per_page in your $args array.
$args = array(
'post_parent' => get_page_by_path( 'products' )->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'any',
'posts_per_page' => '100');

How to render formatted post content in widget in Wordpress

I am new on wordpress and try to make a widget which will only show the last post of particuler category.
TO do this I have given some settings in admin to select category for which admin wants to display last post (post of last ID).
I am using the below code to in widget to display post content:
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
echo $post->post_content;
}
But the above code is showing content in one paragraph or you can say that without format. I have done lot of search and found that, I need to apply "the_content" filter to format the content. So I have done the same as below code:
if ($posts_query->have_posts()) {
$posts = $posts_query->get_posts();
$post = $posts[0];
$content = apply_filters('the_content', $post->post_content);
echo $content;
}
Now the above changes is returning the null string. I have google lot of things but everyone is saying that to use apply filter or use the_content() function. I have tried both solutions but nothing happening.
Can anyone please share the solution for this problem?
please check whether you are getting any text in $post->post_content
by echo $post->post_content
All you need to do is,
$query_arguments = array(
'posts_per_page' => (int) 1,
'post_type' => 'post',
'post_status' => 'publish',
'cat' => (int) $settings['category'],
'order' => 'DESC',
'orderby' => (($settings['display_by'] == 0) ? 'ID' : 'date')
);
$posts_query = new WP_Query( $query_arguments );
while ($posts_query->have_posts()) {
$posts_query->the_post();
echo get_the_content();
}

Categories