• Resolved dados

    (@dados)


    Hi,

    I need to create query to get posts from custom post “video” and term name “music” from default wordpress taxonomy “category”… Anybody know how to do this? I try with some solutions on the net but with no success… I am not sure how to create this 2 queries… and merge it

    Regards,
    Davor

Viewing 1 replies (of 1 total)
  • Thread Starter dados

    (@dados)

    Okay.. I found the solution.. with this query https://wordpress.stackexchange.com/questions/135820/merge-2-args-in-one-wp-query-and-order-it-by-date/179598#179598 but if you use sticky post.. do not forget to add ‘ignore_sticky_posts’ => 1.. and here is query with working example

    <?php
    // first query
    $first_ids = get_posts( array(
        'fields'         => 'ids',
        'posts_per_page' => '6',
        'post_status'    => 'publish',
        'post_type'      => array('your_custom_post_type')
    ));
    
    // second query
    $second_ids = get_posts( array(
        'fields'         => 'ids',
        'posts_per_page' => '6',
        'post_status'    => 'publish',
        'post_type'      => 'post',
        'tax_query'      => array(array(
            'taxonomy'       => 'category',
            'field'          => 'term_id',
            'terms'          => array(7) // your category ID
        ))
    ));
    
    // merging ids
    $post_ids = array_merge( $first_ids, $second_ids);
    
    // the main query
    $query = new WP_Query(array(
        'post_type' => 'any',
        'post__in'  => $post_ids,
        'orderby'   => 'date',
        'order'     => 'DESC',
        'ignore_sticky_posts' => 1
    ));
    
    if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
    echo the_title().'<br>';
    endwhile; endif;
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Merge 2 query… Custom post and Category posts’ is closed to new replies.