$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - WP_Query with all posts in one custom post type and only posts in another custom post type with a specific category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - WP_Query with all posts in one custom post type and only posts in another custom post type with a specific category

matteradmin9PV0评论

I'm trying to do a single WP_Query to get the posts that are in 2 different custom post types but the second post type has to be from a specific taxonomy.

This is what I'm currently doing:

    $query1 = new WP_Query(array(
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => false
));
$query2 = new WP_Query(array(
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'js_videos',
    'tax_query' => array(
        array (
            'taxonomy' => 'video_category',
            'field' => 'tag_ID',
            'terms' => '41',
        )
    ),
    'post_status'      => 'publish',
    'suppress_filters' => false
));

$wp_query = null;
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );

However I need to still order by publish_date after mergining.

I'm trying to do a single WP_Query to get the posts that are in 2 different custom post types but the second post type has to be from a specific taxonomy.

This is what I'm currently doing:

    $query1 = new WP_Query(array(
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => false
));
$query2 = new WP_Query(array(
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'js_videos',
    'tax_query' => array(
        array (
            'taxonomy' => 'video_category',
            'field' => 'tag_ID',
            'terms' => '41',
        )
    ),
    'post_status'      => 'publish',
    'suppress_filters' => false
));

$wp_query = null;
$wp_query = new WP_Query();
$wp_query->posts = array_merge( $query1->posts, $query2->posts );

However I need to still order by publish_date after mergining.

Share Improve this question asked Jan 30, 2019 at 11:49 Baadier SydowBaadier Sydow 1231 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I'm going to take the answer you posted for yourself and suggest a couple of improvements.

Some things I'll address:

  • The 3rd query is unnecessary. There are simpler ways to sort by date. Just make both queries for the full post content and merge the results.
  • Since we're not going to be using have_posts() and the_post() (we'll use setup_postdata()), we should just use get_posts() to keep things a bit simpler. It should also be a bit quicker as it sets some default query args to optimise it since it won't need pagination info.
  • We'll avoid repeating ourselves with the query arguments by re-using a single array of arguments.
  • We won't bother specifying arguments that are just the defaults.

Taking those points into account, the code can just be:

$posts_args = [
    'posts_per_page' => $postsnumber,
    'post_type'      => 'post',
];

$posts = get_posts( $posts_args );

$video_args              = $posts_args;
$video_args['post_type'] = 'js_videos';
$video_args['tax_query'] =  [
    [
        'taxonomy' => 'video_category',
        'field'    => 'tag_ID',
        'terms'    => '41',
    ],
];

$videos = get_posts( $video_args );

$combined = array_merge( $posts, $videos );

usort( 
    $combined, 
    function( $a, $b ) {
        return get_post_time( 'U', false, $b ) - get_post_time( 'U', false, $a );
    }
);

global $post;

foreach ( $combined as $post ) : setup_postdata( $post );
    the_title(); // etc.
endforeach;

wp_reset_postdata();

Never mind.

I got it resolved doing it like this:

    $query1 = new WP_Query(array(
    'fields'            => 'ids',
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'post',
    'post_status'      => 'publish',
    'suppress_filters' => false
));
$query2 = new WP_Query(array(
    'fields'            => 'ids',
    'numberposts'       => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'orderby'          => 'publish_date',
    'order'            => 'DESC',
    'post_type'        => 'js_videos',
    'tax_query' => array(
        array (
            'taxonomy' => 'video_category',
            'field' => 'tag_ID',
            'terms' => '41',
        )
    ),
    'post_status'      => 'publish',
    'suppress_filters' => false
));

$post_ids = array_merge( $query1->posts, $query2->posts);

$args = array(
    'numberposts'      => $postsnumber,
    'posts_per_page'   => $postsnumber,
    'post__in'         => $post_ids, 
    'post_type'        => 'any',
    'orderby'          => 'publish_date', 
    'order'            => 'DESC',
); 

$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query($args);
Post a comment

comment list (0)

  1. No comments so far