Original request:
I also want to allow to display multiple CPT in a widget. I’ve tried the code above but with no success. What should I select in the “Post type” selector in the widget then?
First you will find the correct widget ID. My cases GFPC widget ID is gfpc-widget-4.
Now adding a custom filter for query args. Code will go to functions.php file:
add_filter( 'gfpc_query_args_gfpc-widget-4', 'gd_gfpc_query_args', 10, 2 ); function gd_gfpc_query_args( $qargs, $instance ) { $qargs['post_type'] = array( 'post', 'listing' ); //* assign your custom post type return $qargs; }
I am showing the posts from “post” & “listing” custom post type. You will edit the array( 'post', 'listing' )
based on your post type.
Also I used “gfpc_query_args_gfpc-widget-4” filter. This filter will only work for “gfpc-widget-4” widget ID. If your widget ID is “gfpc-widget-2” then your filter name will be gfpc_query_args_gfpc-widget-2. This way you can change the default query args.
And what is the filter to use between brackets for Taxonomies?
See the Codex for better understand.
Yes, I’ve checked it before but I’m not sure. Woud it be :
$qargs[‘tax_query’] = array ( ‘custom-taxonomy-1’, ‘custom-taxonomy-2’ );
?
That one is wrong.
You can try this way:
$qargs[‘tax_query’] = array(
array(
‘taxonomy’ => ‘YOURTAXONOMYNAME’,
‘field’ => ‘slug’,
‘terms’ => array( ‘custom-taxonomy-1’, ‘custom-taxonomy-2’ ),
)
);
Great! Thanks a lot for your support.
I’d like to complexify the output and to filter by a list of tags as well. What should I add to this function?
See this . It will help you.
Example:
$qargs['tag'] = 'tag1,tag2';
I’ve tried this but it doesn’t show any content:
add_filter( ‘gfpc_query_args_gfpc-widget-7’, ‘gd_gfpc_query_args_hf’, 10, 2 );
function gd_gfpc_query_args_hf( $qargs, $instance ) {
$qargs[‘tax_query’] = array( ‘tag’ => ‘home-featured, home-featured-it’ );
return $qargs;
}
Can you try this once?
Thanks a lot. And what if I want to filter by category name AND post type in the same filter (in functions.php as well)?
Yes. You can do this also. Keep in mind that Category is for ‘Post’ type and Taxonomy is for ‘CPT’.