• Hi, community
    I have a custom loop that shows 20 posts in total on a certain page. The posts are from several categories. That works perfectly. But I need to change it.

    I would like to have:
    20 posts only of the category 2
    all posts of other cats can be displayed without any limitation. Is this possible?

    My loop goes like this:

    global $post;
                $tmp_post = $post;
                $args = array( 
                'numberposts' => '20',
                'orderby' => 'date',
                'order'   => 'DESC',
                'cat' => array(1,2,7,8,9,27, 30, 31),
                'tax_query' => array(
                            array(
                                'taxonomy' => 'post_format',
                                'field' => 'slug',
                                'terms' => array( 'post-format-aside','post-format-video' ),
                                
                            ),
                        )        
     );
                $myposts = get_posts( $args );
                foreach( $myposts as $post ) : setup_postdata($post); 
    

    Can anybody help me with that?
    Thanks!
    Raphael

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    You’d need to do two queries and merge the result arrays (or display them in two sections).

    Thread Starter rabox66

    (@rabox66)

    Thanks. Two queries are easy. But how do I merge the results?

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    That would depend on how you want the results displayed. You could just merge and sort the two resulting arrays if you don’t care about having them separated by category or do all cats then the results for cat=2.

    Thread Starter rabox66

    (@rabox66)

    Hi Steven,
    thanks for the idea of merging. but to be honest I do not see, how this might work. Could you point it out a bit more in detail? I will tell you what I expect, that should be done.

    1) I create two loops. The first is more or less like that:

    args = array( 
                'numberposts' => '20',
                'cat' => array(2),
    

    Whereas the other one is like that:

    
              $args = array( 
                'numberposts' => '-1',
                'cat' => array(1,7,8,9,27, 30, 31),    

    OK, but the merging itself is the problem for me. Here I would be glad if you could give me an idea.
    Thanks!
    Raphael

    • This reply was modified 3 years, 11 months ago by rabox66.
    • This reply was modified 3 years, 11 months ago by rabox66.
    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The arrays you merge are the arrays of post objects returned by get_posts.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Modification of custom loop’ is closed to new replies.