Support » Developing with WordPress » WP_Query two post_types but only return second post type with specific term

  • Is it possible to user WP_Query to return posts of both pt_1 and pt_2, but only for pt_2, it should only return ones with a specific taxonomy term?

    I know I could loop the posts and just display the pt_2’s with the tax term, but that can get messy in the long run.

    • This topic was modified 4 days, 19 hours ago by webdevbytes.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi @webdevbytes,

    You can get posts from multiple post types. Just you have to mention post type in post type args for wp-query. See mentioned code.

    $args = array(
        'post_type' => array( 'post', 'page', 'movie', 'book' )
    );
    $query = new WP_Query( $args );

    Reference: https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters

    Correct me if I get you wrong.

    N.B. Default post type will be any if you use tax query.

    Thread Starter webdevbytes

    (@webdevbytes)

    In your example I would want to get all posts, pages, movies, and also books but only the ones that are written in English (so with a tax term set to english).

    Moderator bcworkz

    (@bcworkz)

    Yeah, filtering in the loop will be much less efficient than building the proper query to start with. You need to add a “tax_query” arg to the $args array. After following the link below, jump to “Taxonomy parameters” from the contents links. (Direct links to that section from here don’t work right) Use one of the examples as a guide for what to do, substituting in your specific values that fit your situation.
    https://developer.wordpress.org/reference/classes/wp_query/

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.