Using a custom Wordpress query to call 5 most recent posts - php

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

Related

Wordpress loop not respecting my arguments

I created a WordPress loop for with specific arguments but it is just ignoring them. For example I like to list only posts but there are also pages listed.
<h1 id="" class="offset"><?php _e('Aktuell','Main'); ?></h1>
<?php
// Restore original Post Data
wp_reset_postdata();
// WP_Query arguments
$args = array (
'post_type' => 'post',
'cat' => '50,47',
'numberposts' => '3',
'posts_per_page' => '3',
'ignore_sticky_posts' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article <?php post_class(); ?>>
</article>
<?php }
} else {
// no posts found
}
?>
</div>
This code is working correctly on my site. Perhaps try doing the loop as
<article <?php post_class(); ?>>
<?php echo get_the_title(); ?>
</article>
and confirm if the titles are definitely not what you're expecting

wp_reset_query not resetting

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

How can I make this Php WP_Query returning categories only once?

This loop returns the categories of each post, showing it multiple times. Instead, I want to show categories once, this is a filter by category functionality.
<div id="filter-box" class="filter-box">
<?php
$args = array (
'post_type' => "post",
'post_status' => "publish",
'order' => 'ASC',
'orderby' => 'title' ,
'posts_per_page' => -1);
$all_query = new WP_Query($args);
if ($all_query->have_posts()):
while ($all_query->have_posts()):
$all_query->the_post();
?>
<?php get_cat($post->ID); ?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
</div>
This is the function from functions.php
function get_cat($post_id) {
$category_detail=get_the_category($post_id);
foreach($category_detail as $cd) {
echo $cd->cat_name;
}
}
Want to display the categories only once as they appear once for each of the posts.
Please modify you code as:
<?php
$args = array (
'post_type' => "post",
'post_status' => "publish",
'order' => 'ASC',
'orderby' => 'title' ,
'posts_per_page' => -1);
$all_query = new WP_Query($args);
$tempArr = array();
if ($all_query->have_posts()):
while ($all_query->have_posts()):
$all_query->the_post();
if(!in_array($post->ID , $tempArr)){ // check if array has ID not display
array_push($tempArr, $post->ID); //push postID in array
?>
<?php get_cat($post->ID); ?>
<?php
}
endwhile;
endif;
wp_reset_postdata();
?>
Explanation: Please declare an array as $tempArr = array(); and then check if $temArr array has that postID if not then display category and push it in array.

How to write more efficient fallback for empty wp_query in Wordpress

I have a WooCommerce store where I want to display a featured image & heading of one of the following (in order):
Featured Product
If no featured product, then sticky post
If no sticky post, then most recent post
But I also want to write efficient code. How do I simplify this and remove redundant PHP and HTML?
/* START FEATURED PRODUCT QUERY */
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_query' => array(
'key' => '_featured',
'value' => 'yes'
),
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
} else {
/* START FALLBACK POST QUERY */
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
}
The second WP_Query has the exact same HTML output, just different $args
I’m writing a similar query and I’m not sure you can make the query much more efficient in Wordpress than you already have. The only thing I did differently was to make the output of the posts a function so that it calls the same code. This’ll make it easier to update.
Also since you’re only querying one meta field in the first query I switched to a simple custom field query.
// Function to output posts
function output_posts( $query ){
while( $query->have_posts() ) {
$query->the_post();
echo '<a href="' . get_permalink() '" id="featured-blog-post">';
the_post_thumbnail( 'full' );
the_title( '<h2>', '<span>»</span></h2>' );
the_excerpt();
echo '</a>';
}
wp_reset_postdata();
}
// Featured query
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
);
$featured = new WP_Query( $args );
// If featured has posts
if( $featured->have_posts() ) {
// Output
output_posts( $featured );
// Else fallback
} else {
// Fallback query
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1,
);
$fallback = new WP_Query( $args );
// If fallback has posts
if ( $fallback->have_posts() ){
// Output
output_posts( $fallback );
}
}

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>

Categories