WP_Query To Get Custom Posts Type Posts By A Taxonomy Term With Fallback Posts

/**
* Get Posts By Taxonomy.
*
* 1. Make a query by taxonomy and return if we have sufficient results.
* 2. If we don't make a query to get default posts ( get extra posts so that we can remove previously
* found posts, to avoid 'post__not_in' query )
* 3. Merge the found post ids with previous ones and make return a fresh query by those ids in that order.
*
*
@param int $number The number of posts to return.
*
@param string $taxonomy Taxonomy.
*
@param string $term_slug Term slug
*
*
@return WP_Query
*/
function get_posts_by_taxonomy_query( int $number = 4, string $taxonomy = 'your-taxonomy', $term_slug = 'your-term-slug' ) {

$args = [
'posts_per_page' => $number,
'post_type' => 'your-post-type',
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false,
'tax_query' => [
[
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_slug,
],
],
];

$post_tax_query = new WP_Query( $args );
$post_by_tax_ids = wp_list_pluck( $post_tax_query->posts, 'ID' ); // So that we don't have to do query twice,when we have sufficient results
$the_post_count = count( $post_by_tax_ids );

// If we have sufficient no of results, early return.
if ( $number == $the_post_count ) {
return $post_tax_query;
}

// Otherwise get default posts and merge the two set of ids.
$args['tax_query'] = [];
$args['fields'] = 'ids';
$args['posts_per_page'] = ( $number - $the_post_count ) + $the_post_count; // So that duplicates could be removed later.
$post_default_post_query = new WP_Query( $args );
$ids = array_merge( $post_by_tax_ids, $post_default_post_query->posts );

// Get the posts and with retained order.
$all_posts_args = [
'post__in' => $ids,
'posts_per_page' => $number,
'orderby' => 'post__in',
];

return new WP_Query( array_merge( $args, $all_posts_args ) );

}

--

--

--

👤 Full Stack Developer at rtCamp, Speaker, Blogger, YouTuber, Wordpress, React, Node, Laravel Developer http://youtube.com/ImranSayedDev

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Snowflake Connector Spark Zepplin

Want to Make a Native Mobile Game? Try Solar2D

Practical Apache Spark in 10 minutes. Part 3 — DataFrames and SQL

WHY THE KEPLERSWAP?

How I made my own citation engine to spite the Chegg Citation Complex

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Reduce Cost and Increase Productivity with Value Added IT Services from buzinessware — {link} -

Let’s Have Fun With Interpreters and Bytecode VMs— Chapter 1

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Imran Sayed

Imran Sayed

👤 Full Stack Developer at rtCamp, Speaker, Blogger, YouTuber, Wordpress, React, Node, Laravel Developer http://youtube.com/ImranSayedDev

More from Medium

MERN STACK Web Development

CreateStudio Step-by-step tutorial

Understanding the Google CWV Sources ? 🤔

How to Earn Money using Honeygain App? Fastest and Easy Way