ubermenu_dynamic_posts_args

Knowledgebase Docs » UberMenu 3
USEFUL? 14
UberMenu 3

The ubermenu_dynamic_posts_args filter allows you to filter the post query arguments array just before it is passed to get_posts for Dynamic Posts.

Here’s an example of using the filter. We’ll target a Dynamic Post item with the ID 562. The demonstrates how you’d alter the query arguments, in this example changing the offset or the sorting.

function um_filter_dynamic_post_args( $post_args , $menu_item_id ){

	//Target menu item 562
	if( $menu_item_id == 562 ){

		//Change offset to 5
		$post_args['offset'] = 5;

		//Change orderby to random
		$post_args['orderby'] = 'rand';
	}

	return $post_args;

}
add_filter( 'ubermenu_dynamic_posts_args' , 'um_filter_dynamic_post_args' , 10 , 2 );

ubermenu_dynamic_posts_args

Your registered callback will be called once for each dynamic post item. The array you return will be passed as query arguments to the get_posts() function. You have access to the query arguments (with parameters set in the menu item settings already present) and the ID of the Dynamic Posts item.

$post_args
This is an array of query arguments to be passed to get_posts(). The filter callback should return this. For a full list of parameters that can be passed, please see The WordPress Codex: WP Query Parameters
$menu_item_id
This is the ID of the Dynamic Posts menu item.

WPML + category__and corner case

While not actually related to UberMenu, note that if you are using the category__and post arg along with WPML, WPML won’t process this the same way as WordPress core – it won’t combine your category__and value with your cat value, so you need to place all categories in the category__and argument. e.g.

if( isset( $post_args['cat'] ) ){
   $post_args['category__and'] = array( 256 , $post_args['cat'] );
   unset( $post_args['cat'] );
}