Set Up a Custom Query for a Specific Custom Post Type

Using the query filter in Six/Ten Press Featured Content, you can easily modify the query for any custom post/content type on your site. For example, for The Soul Care Project, I created a custom content type for retreats and workshops, which are ordered by a custom field, which is the date of the event. I’ve added this filter to the retreats plugin and the widget will now properly show the next upcoming retreat(s):

add_filter( 'sixtenpressfeaturedcontent_query_args', 'prefix_modify_widget_query', 10, 2 ); /** * Set a custom order on the widget output. * Outputs retreats for starting with the next upcoming retreat. * @param $query_args * @param $instance * * @return mixed */ function prefix_modify_widget_query( $query_args, $instance ) { if ( 'retreat' === $instance['post_type'] ) { $query_args['orderby'] = 'meta_value post_date'; $query_args['meta_key'] = '_retreats_start_date'; $query_args['meta_value'] = strtotime( date( 'Ymd' ) ); $query_args['meta_compare'] = '>='; $query_args['order'] = 'ASC'; } return $query_args; }
Code language: PHP (php)

For another plugin, I’ve created a custom content type which supports child posts, similar to pages. For the widget, we want only the parent level posts to display, even if the widget order is set to show a random post from that content type. Here’s how that filter is configured:

add_filter( 'sixtenpressfeaturedcontent_query_args', 'prefix_show_parent_posts', 10, 2 ); /** * For a ministry custom post type which allows child posts/pages, make sure the * widget only shows top level posts/ministries. * @param $query_args * @param $instance * * @return mixed */ function prefix_show_parent_posts( $query_args, $instance ) { if ( 'ministry' !== $instance['post_type'] ) { return $query_args; } $query_args['post_parent'] = 0; return $query_args; }
Code language: PHP (php)
Return to Six/Ten Press Featured Content