wp_reset_query not resetting - php

I am trying to query several different types of post on the same page. When I try to query the second time (Essays), nothing shows up, meaning my if ($arr_posts->have_posts()) is evaluating as false. The first query and the third and fourth query are working fine. And the second query was working until I added this Interview query before it. And even when I commented it out, it still stopped showing up. What am I missing? And mwp_interview is a custom post type.
<!--Latest Interview-->
<?php
$args = array(
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY INTERVIEW POST
</div>
<?php
endif; ?>
<!--Latest Essay-->
<?php
wp_reset_query();
wp_reset_postdata();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Essays in Discipleship',
'posts_per_page' => 1
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY ESSAY POST
</div>
<?php
endif; ?>
<!--Latest Special Series-->
<?php
$ss_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Special Post',
'posts_per_page' => 1,
);
wp_reset_query();
$ss_arr_posts = new WP_Query( $ss_args );
if ( $ss_arr_posts->have_posts() ) :
$ss_arr_posts->the_post();
?>
<div>
DISPLAY SPECIAL SERIES POST
</div>
<?php
endif;
?>
<!--Latest Podcast-->
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'Podcast',
'posts_per_page' => 1,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<div>
DISPLAY PODCAST
</div>
<?php
endif;
?>

As per the Wordpress documentation WP_Query Category Parameters.
category_name (string) – use category slug.
The argument's name category_name is actually misleading, category_name is referring to the category SLUG, NOT the actual category NAME.
<?php
$args = [
'posts_per_page' => 1,
'post_status' => 'publish',
'post_type' => 'mwp_interview',
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo "<hr>";
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "interview" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'essays-in-discipleship', // ... must be set set to "Essays In Discipleship" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Essays In Discipleship" just yet!';
endif;
wp_reset_postdata(); ?>
<?php
$args = [
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'special-post', // ... must be set to "Special Post" category slug
'posts_per_page' => 1,
];
$query = new WP_Query( $args );
if( $query->have_posts() ):
$i = 0;
while( $query->have_posts() ): $query->the_post();
$i++;
if ( $i > 1 )
echo '<hr>';
the_title( '<h1>', '</h1>' );
endwhile;
else:
echo 'No "Special Post" just yet!';
endif;
wp_reset_postdata(); ?>

Related

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();
}
?>

How to get child posts of current custom post and order it by custom field number?

I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>

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();

wordpress pagination not showing

I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );

using in_category rather than query_posts in wordpress

In wordpress I have a page template called news that I want to display all the posts from one category - 'News'. I don't want to use the category.php because there is a massive blog on the site already.
query_posts('cat=145');
while ( have_posts() ) : the_post();
//do something
endwhile;
Works fine but I have read that query_posts has drawbacks (like speed)
I tried doing this but it just showed me nothing:
while ( have_posts() ) : the_post();
if ( in_category( '145' ) ) : //also tried 'News'
//do something
Why doesn't' in_category work here?
please try this code:
$args = array('post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news' // please pass here you news category slugs
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post);
endwhile;
wp_reset_postdata();
You could WP Query for achieving your requirement.
Documentation: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Example:
<?php
$args = array(
'cat' => 145,
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try to use get_posts() function:
$get_p_args = array('category'=> 145,'posts_per_page'=>-1);
$myposts = get_posts( $get_p_args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach;
wp_reset_postdata();?>
Try this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 6, // limit of posts
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'lang' => 'en', // use language slug in the query
) );
query_posts( $args );

Categories