Multiple custom post loops on page - php

I'm unable to get a second or third loop to display after the first loop is displayed. I've been trying different things for the past two hours but I'm running out of ideas.
This is what I have right now --
<?php
$type = 'new_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
The second loop is like so and is right under the first loop shown above...
<?php
$type = 'used_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
?>
<?php while (have_posts()) : the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
In my attempts to duplicate that in the same page with a different type set, the second loop is not displaying at all. If I erase the first loop, it works. I'm hoping someone can please provide some guidance.
Thanks!

<?php
$type = 'new_trucks';
$args=array(
'orderby' => 'rand',
'post_type' => $type,
'paged' => $paged,
'posts_per_page' => 3,
);
$wp_query = null;
$wp_query = new WP_Query($args);
?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
...
<?php endwhile; /* End loop */ ?>
<?php wp_reset_query(); ?>
is how I have done it in the past

Related

Why does posts_per_page doesn't work in Wordpress?

I'm trying to take 12 posts per a page, but it displays only 4 posts.
Has anyone encountered this problem?
Current Code
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'posts_per_page' => 12,
'order' => 'DESC',
'paged' => $paged,
);
$lists = new WP_Query($args);
echo $lists->post_count; // this outputs 12!
<ul>
<?php if ($lists->have_posts()) : ?>
<?php while ($lists->have_posts()) : $lists->the_post(); ?>
<li>
<div>
<?php the_title(); ?>
</div>
</li>
<?php endwhile; ?>
<?php endif;
wp_reset_postdata(); ?>
</ul>
Please add wp_reset_query(); above $lists = new WP_Query($args);
wp_reset_query();
$lists = new WP_Query($args);

Using a custom Wordpress query to call 5 most recent posts

I am trying to pull the 5 most recent posts of a custom post type using a WP_query. Does the code below look correct? And do I need to use wp_reset_postdata at the end?
<?php
$args = array(
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
);
$most_recent = new WP_Query( $args );
?>
<?php if( $most_recent->have_posts() ) ?>
<?php while( $most_recent->have_posts() ) : $most_recent->the_post() ?>
<div class="webinar">
<h2><?php echo get_the_title(); ?> </h2>
<h3><?php echo get_the_date(); ?></h3>
<p><?php echo get_the_excerpt(); ?></p>
</div>
<?php endwhile; ?>
<?php endif ?>
You don't need to use wp_reset_postdata() unless you are using WP_Query again in the same page. Usage of wp_reset_postdata() is need to set the post data back
Example
<?php
// The 1st Query
$args = [
'post_type' => 'webinar_post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'post_date',
'order' => 'DESC',
];
$most_recent = new WP_Query( $args );
if ( $most_recent->have_posts() ) {
// The Loop
while ( $most_recent->have_posts() ) { $most_recent->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
// Updating `$args`
$args['orderby'] = 'post_title'
$args['order'] = 'ASC'
/* The 2nd Query */
$most_recent2 = new WP_Query( $args );
if ( $most_recent2->have_posts() ) {
// The 2nd Loop
while ( $most_recent2->have_posts() ) { $most_recent2->the_post();
// your code
}
// Restore original Post Data
wp_reset_postdata();
}
?>

Not getting paging links when using WP_Query in Wordpress

I am not getting paging links when using WP_Query in WordPress. I am trying to fetch all the products on the page and show them in page by page manner. For this I want to show the paging links on the bottom. But paging links are not showing.
Below is the link - here
And the code for this is:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => 3,
'paged' => $paged
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<? wp_reset_query(); ?>
next_posts_link(); and previous_posts_link(); and not showing links.
Please help.
Use this code instead of of above code.
<?php
$temp = $wp_query;
$wp_query= null;
$postsPerPage = 3;
$argsev = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => $postsPerPage,
'paged' => $paged
);
$wp_query = new WP_Query($argsev);
while ( $wp_query->have_posts() ) : $wp_query->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile; ?>
<?php next_posts_link(); ?>
<?php previous_posts_link(); ?>
<?php
$wp_query = null; $wp_query = $temp;
wp_reset_query();
?>

Custom post_per_page not working

I'm having troubles displaying lists of custom_post_type on my website.
I can get the posts but what is not working is "post_per_page". It gets ignored and I get every post of that type.
Here is the code I have on my home page (it's not working elsewhere too):
<?php
// 1ST
$args = array(
'post_type' => 'custom_post_1',
'post_per_page' => 1,
);
$custom_post_1 = new WP_Query($args);
if ($custom_post_1->have_posts()) : while ($custom_post_1->have_posts()) : $custom_post_1->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php
// 2ND
$args = array(
'post_type' => 'custom_post_2',
'post_per_page' => 3,
);
$custom_post_2 = new WP_Query($args);
if ($custom_post_2->have_posts()) : while ($custom_post_2->have_posts()) : $custom_post_2->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
<?php
// 3RD
$args = array(
'post_type' => 'custom_post_3',
'post_per_page' => 1,
);
$custom_post_3 = new WP_Query($args);
if ($custom_post_3->have_posts()) : while ($custom_post_3->have_posts()) : $custom_post_3->the_post(); ?>
// My content
<?php endwhile; endif; wp_reset_postdata(); ?>
Thanks in advance!
posts_per_page and not post_per_page my friend
Please refer to the pagination parameter of WP_Query, as per that it will be posts_per_page instead post_per_page.
https://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
So as per that your query will be as follows,
$args = array(
'post_type' => 'custom_post_1',
'posts_per_page' => 1,
'post_status' => 'publish',
);
$custom_post_1 = new WP_Query($args);
Hope this one helps :)

Why wp_query loop still considers hidden categories and 'post__not_in' in pagination?

I have three loops on same page.
The first loop displays the most recent post of category "highlight".
The second loop displays others posts of this same category in chronological order.
The third loop displays all the posts, except the posts in others loops.
All works fine, but in pagination (pagenavi), the max_num_pages considers all the posts, ignoring criterias like 'post__not_in' or 'cat'.
If I use this loop, the last page of pagenavi stills in blank (counts hidden posts yet, but don't shows them):
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
And If I use this loop (loop 3), the last page of pagenavi shows the "hidden" posts:
if (have_posts()) : while (have_posts()) : the_post();
How to force the wp_query loop to exclude the hidden posts of the max_num_pages count?
//loop 1
<?php
$loop1 = new WP_query(array(
'category_name' => 'highlight',
'posts_per_page' => 1,
));
if($loop1->have_posts()) : $firstPosts = array(); while($loop1->have_posts()) : $loop1->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 2
<?php
$loop2 = new WP_query(array(
'post__not_in' => $firstPosts,
'category_name' => 'highlight',
'posts_per_page' => 2,
));
if($loop2->have_posts()) : while($loop2->have_posts()) : $loop2->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 3
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
//other stuff
//pagenavi
<?php wp_pagenavi("", "", array(
'query' => $loop3,
'first_text' => 'lorem ipsum',
'last_text' => 'lorem ipsum',
)); ?>
If you're using WP-PageNavi, there is a special way to use it with custom queries. For example:
$my_query = new WP_Query(
array(
'tag' => 'foo',
'paged' => get_query_var('paged')
)
);
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
So make sure you are passing your custom query object into the wp_pagenavi() function call.
~Edit~
Here's the link to the documentation: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
~Edit 2~
Try this code that has been customized for your particular application:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
...
wp_pagenavi( array( 'query' => $loop3 ) );
wp_reset_postdata();

Categories