I would like to fetch my posts of type 'news', 'videos', 'webinars', 'whitepapers', 'testimonials', and 'landing-pages' and apply a "filter" to only get the landing-pages type with the taxonomy 'landing_pages_display_home' having 'displayed-on-home-page' as terms and 'slug' as the field and my other post type.
This is my current query, it does return the landing-pages with the defined taxonomy and terms, but I do not get the other post types.
$args = array(
'posts_per_page' => 6,
'post_status' => 'publish',
'order' => 'DESC',
'post_type' => array(
'news',
'videos',
'webinars',
'whitepapers',
'testimonials',
'landing-pages'
),
'tax_query' => array(
array(
'taxonomy' => 'landing_pages_display_home',
'field' => 'slug',
'terms' => 'displayed-on-home-page',
),
),
);
$resources = new WP_Query($args);
My question is, how can I get the posts of type 'news', 'videos', 'webinars', 'whitepapers', 'testimonials' and the 'landing-pages' posts having 'displayed-on-home-page' as terms and 'slug' as the field?
There is my taxonomy :
register_taxonomy(
'landing_pages_display_home',
array('landing-pages'), /* if you change the name of register_post_type( 'custom_type', then you have to change this */
array(
'hierarchical' => true, /* if this is true, it acts like categories */
'labels' => array(
'name' => __( 'Categories', 'domain_name' ), /* name of the custom taxonomy */
'singular_name' => __( 'Categories', 'domain_name' ), /* single taxonomy name */
'search_items' => __( 'Search Categories', 'domain_name' ), /* search title for taxomony */
'all_items' => __( 'All Categories', 'domain_name' ), /* all title for taxonomies */
'edit_item' => __( 'Edit Categories', 'domain_name' ), /* edit custom taxonomy title */
'add_new_item' => __( 'Create new Categorie', 'domain_name' ), /* add new title for taxonomy */
'parent_item' => __('Parent Categories', 'domain_name'), // parent title for taxonomy
'parent_item_colon' => __('Parent Categories:', 'domain_name'), // parent taxonomy title
'update_item' => __('Update Categories', 'domain_name'), // update title for taxonomy
),
'show_admin_column' => true,
'show_ui' => true,
'query_var' => true,
'show_in_rest' => true, // Needed for tax to appear in Gutenberg editor.
'rewrite' => array('slug' => 'landing_pages_display_home'),
)
);
Let me know if you want more details. I'm pettry new with wordpress, so thanks for any help that you give me !
EDIT :
I managed to do something like this :
$args_landing_pages = array(
'posts_per_page' => 6,
'post_status' => 'publish',
'order' => 'DESC',
'post_type' => 'landing-pages',
'tax_query' => array(
array(
'taxonomy' => 'landing_pages_display_home',
'field' => 'slug',
'terms' => 'displayed-on-home-page',
),
),
);
$query_landing_pages = new WP_Query($args_landing_pages);
$landing_page_ids = wp_list_pluck($query_landing_pages->posts, 'ID');
$args_others = array(
'posts_per_page' => 6,
'post_type' => array(
'news',
'videos',
'webinars',
'whitepapers',
'testimonials'
),
'order' => 'DESC',
'post_status' => 'publish',
);
$query_others = new WP_Query($args_others);
$other_post_ids = wp_list_pluck($query_others->posts, 'ID');
// Combine IDs
$combined_post_ids = array_merge($landing_page_ids, $other_post_ids);
// Construct the final request THAT NOT REQUEST to the db, so we can preserve performences and compatibility with wordpress functions
$args_combined = array(
'post_type' => 'any',
'post_status' => 'publish',
'posts_per_page' => 6,
'post__in' => $combined_post_ids,
'orderby' => 'post__in'
);
$resources = new WP_Query($args_combined);
But it needs 2 requests to work so that's not performance friendly... So I still need help with this :(