0

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 :(

1 Answer 1

0

tax_query with an 'OR' relation. This means that the query will fetch posts that meet either condition: they are 'landing-pages' with the specified taxonomy terms, or they are any of the other specified post types without additional taxonomy constraints.

$args = array(
'posts_per_page' => 6,
'post_status'    => 'publish',
'order'          => 'DESC',
'post_type' => array('news', 'videos', 'webinars', 'whitepapers', 'testimonials', 'landing-pages'), // Include all your target post types
'tax_query' => array(
    'relation' => 'OR', // Important for combining different post type conditions
    array(
        'taxonomy' => 'landing_pages_display_home',
        'field'    => 'slug',
        'terms'    => 'displayed-on-home-page',
        // Ensure only 'landing-pages' post type is affected by this taxonomy query
        'include_children' => false,
        'operator'  => 'IN'
    ),
    array(
        'operator' => 'EXISTS', // This allows posts of other types without further conditions
    )
  )
);

$resources = new WP_Query($args);
1
  • Hi, thank you for your reply but as I know, this use is incorrect because EXISTS and NOT EXISTS are operators that should be used to test the existence (or non-existence) of a term in a given taxonomy, and they need to specify a taxonomy. I still tested your answer, and I got only landing pages that have the term, not the others resources...
    – WypSteur
    Mar 18 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.