WordPress.org

Ready to get started?Download WordPress

Forums

Merge 2 post types from different categories (2 posts)

  1. Niels van Renselaar
    Member
    Posted 2 years ago #

    Hello,

    I have 2 post types, "news" and the regular "post". I want to merge these 2, so I build a WP_Query:

    $news = new WP_Query(array("post_type" => array("news", "post"), "showposts" => 2));

    That works very well. But; from "post" I want only items places in categorie 10, so I tried this:

    $news = new WP_Query(array( "post_type" => array("news", "post"), "showposts" => 2, "category__in" => array(10) ));

    Results: nothing for "news" gets displayed because none of those messages apply the the "category__in" selection. How do I get category__in to apply only to posts?

  2. Niels van Renselaar
    Member
    Posted 2 years ago #

    Well, I now have something working, so for people in need:

    $querystr = "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts, $wpdb->term_relationships
    WHERE $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_relationships.object_id = $wpdb->posts.ID
    AND ( ($wpdb->posts.post_type = 'post' AND $wpdb->term_relationships.term_taxonomy_id = 10) OR ($wpdb->posts.post_type = 'news'))
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 2
    ";
    
    $pageposts = $wpdb->get_results($querystr, OBJECT);

    Where 10 is your term ID

Topic Closed

This topic has been closed to new replies.

About this Topic