1. Home
  2. Docs
  3. Responsive Posts Carousel
  4. Developers
  5. rpc_query_args

rpc_query_args

It’s a filter hook, that applies to the WP_Query args before rendering. You can use this filter for advanced post filtration, taxonomy or terms related queries.

Example

add_filter( 'rpc_query_args', 'rpc_change_query' );

function rpc_change_query($args){

	$args['tax_query'] => array(
		array(
			'taxonomy' => 'people',
			'field'    => 'slug',
			'terms'    => 'bob',
		),
	);

	return $args;
}

 

A Complex Structure

Let’s use it further for complex queries, suppose you want to display 4 latest posts on a slider from 3 different categories.

Example Scenario:

  • 2 latest posts from Category having ID 87
  • 1 latest post from category having ID 96
  • 1 latest post from category having ID 100

So, you have to paste the following code in the functions.php file of your theme/child theme and it will do the job.

add_filter( 'rpc_query_args', 'rpc_custom_carousel_query', 10, 1 );

function rpc_custom_carousel_query($args){
	$post_ids = array();

	// Get 2 posts from category 87
	$p_ids = rpc_latest_posts_from_cat('87', '2');
	$post_ids = array_merge($post_ids, $p_ids);

	// Get 1 post from category 96
	$p_ids = rpc_latest_posts_from_cat('96', '1');
	$post_ids = array_merge($post_ids, $p_ids);

	// Get 1 post from category 100
	$p_ids = rpc_latest_posts_from_cat('100', '1');
	$post_ids = array_merge($post_ids, $p_ids);

	// New args with custom posts by IDs
	$args = array(
		'post__in' => $post_ids,
		'ignore_sticky_posts' => true,
		'posts_per_page' => -1,
	);

	return $args;
}

function rpc_latest_posts_from_cat($cat, $total){
	$post_ids = array();
	$args = array(
		'order' => 'DESC',
		'orderby' => 'date',
		'cat' => $cat,
		'posts_per_page' => $total,
		'ignore_sticky_posts' => true,
	);

	$the_query = new WP_Query( $args );

	if ( $the_query->have_posts() ) {
		while ( $the_query->have_posts() ) {
			$the_query->the_post();
			$post_ids[] = get_the_id();
		}
		/* Restore original Post Data */
		wp_reset_postdata();
	}

	return $post_ids;
}

You can also modify it as you like.