I want to query two post types: 'projects' all of them and 'posts' by taxonomy 'light'. Does someone know how can this be achieved?



I solved my problem by automatically adding the tag 'light' to all my projects. And then query all the post and projects with the light tag like this:

Query post by taxonomy tag 'light':

$args = array(
   'post_type'      => array( 'projects' , 'post' ),
   'tax_query'      => array(
   'relation'       => 'OR',
          'taxonomy'    => 'post_tag',
          'field'       => 'name',
          'terms'       => 'light'
    'post_status'   => 'publish'

query_posts( $args );

Add tag 'light' to all projects on publish:

add_action('publish_projects', 'projects_post_type_tagging', 10, 2);

function projects_post_type_tagging($post_id, $post){
    wp_set_post_terms( $post_id, 'light', 'post_tag', true );

It's not the most elegant solution but it works. If theres a better way to solve this problem i've would like to know.


You can use the WP_Query paramether tag__in to show posts associated with that tag. You can read it here : https://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters


Have you registered any custom taxonomy for projects and posts? what is the slug of that taxonomy?

$args = array(
             'post_type' => array('post_type_1', 'post_type_2'), 
             'posts_per_page' => -1,
             'post_status' => 'publish',
             'taxonomy-slug' => 'category-slug'
$query = new WP_Query($args);
  • Nope, the taxonomy I use for the post are the 'tags'. The term of this tag should be 'light'. – Kris Oct 24 '16 at 12:33
  • Fixed it with adding automatically adding the tag 'light' to projects. – Kris Oct 24 '16 at 13:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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