phpwordpresspostcategories

Get post type and category


On my frontpage (index.php) I would like to display all posts of the type 'event' as well as the category 'main' of the type 'post'. How can I merge those two conditions? In my current code, I can filter the two post-types but not the category 'main'.

<?php global $wp_query; $args = array_merge( $wp_query->query, array( 'post_type' => array('post','event') ));
query_posts( $args ); ?>

Solution

  • Just change the $args to this:

    <?php
    $args = array(
        'post_type' => array('post','event'),
    
        'tax_query' => array(
              'relation' => 'OR',
           array(
             'taxonomy' => 'category',
             'terms' => 'main',
             'field' => 'slug'
           ),
           array(
             'taxonomy' => 'event_tag', // this needs to be whatever custom taxonomy you have declared for your custom post type.
             'terms' => 'main',
             'field' => 'slug'
           ),
        )
    );
    ?>