What's the best way to bring posts to the top of the query if it's in a category?

Hello,
I need help with my project. I'm using WordPress to create an online newspaper where I use press-this to post links. I don't want to use sticky posts as it's not on press-this and I won't be visiting the admin as little as possible. What's the best way to bring posts to the top if it's in a category called "Group"? I've tried to do a query in a query but it got too complex for me that I had to start over.

This is my loop so far:

<?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published'))));
if (have_posts() ) : $duplicates  = []; while (have_posts() ) : the_post(); ?> 

<?php endwhile; endif; ?>

What got me confused is the rest of the query as I'm going to add more categories with different styles. This is my last query:

<?php $args = array(
'tax_query' => array(
array(
    'taxonomy' => 'post-status',
    'field' => 'slug',
    'terms' => array ('post-status-published')
    )
)
); $query = new WP_Query( $args );?>
<div class="container">
<?php if ( have_posts() ) {
while( $query->have_posts() ) {
    $query->the_post(); ?>

    <?php if (in_category('First') ) { ?>
        <?php the_title();?>
    <?php }

}  // end first while loop 

rewind_posts(); // rewind loop so we can rerun it ?>

<?php while( $query->have_posts() ) { // Start second while loop
    $query->the_post(); ?>

     <?php if (in_category('Second') ) { ?>
        <?php the_title();?>
    <?php }

}  // end first while loop 

rewind_posts(); // rewind loop so we can rerun it ?>

<?php while( $query->have_posts() ) { // Start second while loop
    $query->the_post(); ?>

    <?php the_title();?>

<?php } // end second while loop

rewind_posts(); // rewind loop so we can rerun it

} // End your if statement ?>

What I'm trying to do is if posts are in a group, that group moves to the top and the rest are split into two columns.

An example of what I'm trying to do:
- If Group A:
<div class="group">
-Title
-Title
-Title
</div>
<div class="bottom">
Title | Title
Title | Title
Title | Title
</div>

Thanks for the help.

  • Gregory

    That didn't help but maybe you can solve this?

    Is there a way to have this code work under in_category?

    My code:

    <?php if ( in_category( 'First' ) ) : ?>
    <?php the_title();?><br>
    <?php $duplicates[] = get_the_ID(); ?>
    <?php endif; endwhile; ?>
    <?php rewind_posts(); ?>
    <?php while ( have_posts() ) : the_post(); ?>
     <?php if ( in_array( get_the_ID(), $duplicates ) ) continue; ?>
     <?php the_title();?><br>

    This is the full code so far:

    <?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published'))));
    if (have_posts() ) : $duplicates  = []; while (have_posts() ) : the_post(); ?> 
    
    <?php if ( in_category( 'First' ) ) : ?> <?php the_title();?><br> <?php $duplicates[] = get_the_ID(); ?> <?php endif; endwhile; ?> <?php rewind_posts(); ?> <?php while ( have_posts() ) : the_post(); ?> <?php if ( in_array( get_the_ID(), $duplicates ) ) continue; ?> <?php the_title();?><br>
    
    <?php endwhile; endif; ?>

    Thanks

  • Ash

    Hello Gregory

    Would you please try the following code?

    <?php
    $args = array(
                'tax_query' => array(
                    array(
                        'taxonomy' => 'post-status',
                        'field' => 'slug',
                        'terms' => array( 'post-status-published' )
                    )
                )
            );
    
    if ( have_posts() ) :
        while (have_posts() ) :
            the_post();
            if ( in_category( 'First' ) ) :
                the_title();
                ?>
                <br>
                <?php
            endif;
        endwhile;
    
        rewind_posts();
    
        while ( have_posts() ) :
            the_post();
            if ( in_category( 'First' ) ) continue;
            the_title();
            ?>
            <br>
            <?php
        endwhile;
    endif;
    ?>

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Gregory

    This is starting to look great but need help with the bottom part of the code:

    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <?php if ( in_category( 'First' ) ) continue; ?>
            	<?php the_title(); ?><br>
        	<?php endwhile; ?>
        <?php endif; ?>

    How do you add multiple items in the bottom loop like if in_category 'First', in_category 'Group A', etc. and ignore the first loop? Example of what I'm trying to do:

    <?php if (in_category('Group A'))  continue :?>
    				<?php the_title();?><br />
    			<?php endif;?>
    	<?php if (in_category('First'))  continue :?>
    				<?php the_title();?><br />
    			<?php endif;?>

    Thanks

  • Gregory

    I apologize for not fully explaining my words so I'll try again.

    This is what I have so far:

    <?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published')))); $query = new WP_Query( $args );?>
    	<?php if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
    
    		<?php if (in_category(array('First','Group A'))): ?>
    			<?php if (in_category('Group A')) :?>
    				<?php the_title();?><br />
    			<?php endif;?>
    			<?php if (in_category('First')) :?>
    				<?php the_title();?><br />
    			<?php endif;?>
    		<?php endif; ?>
    
        <?php endwhile; endif; ?>
    
        <?php $query->rewind_posts();?>
    
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <?php if ( in_category( 'First' ) ) continue; ?>
            	<?php the_title(); ?><br>
        	<?php endwhile; ?>

    What it does so far is that if a post are in the "First" category, it is brought to the top of the query and the second loop shows the remaining posts minus the post in the "First" category.

    What I need help with is in the second loop:

    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <?php if ( in_category( 'First' ) ) continue; ?>
            	<?php the_title(); ?><br>
        	<?php endwhile; ?>

    How do I more "in_category()" with "continue"? For example: how do I add "if (in_category(Group A))continue;?>"?

    Thanks for the help.

  • Gregory

    Thank you so much for help. I'm starting to feel like I'm finally getting work done.

    I'm having problem with out of order posts in a certain group. I have three categories: group A,B and C. I have one post is group A, two posts in group B and two posts in group C. This is how it's suppose to look:

    - Group A post
    - Group B post
    - Group B post
    - Group C post
    - Group C post

    However it's posted out of sync, based on publish date. And ideas on how to fix this? This is how it tooks:
    - Group A post
    - Group C post
    - Group B post
    - Group B post
    - Group C post

    Code:

    <?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published')))); $query = new WP_Query( $args );?>
    	<?php if ($query->have_posts() ) : $duplicates = []; while ($query->have_posts() ) : $query->the_post(); ?>
    
    		<?php if (in_category(array('First','Group A','Group B','Group C'))): ?>
    			<?php if (in_category('Group A')) :?>
    				A - <?php the_title();?><br />
    			<?php endif;?>
    			<?php if (in_category('Group B')) :?>
    				B - <?php the_title();?><br />
    			<?php endif;?>
    			<?php if (in_category('Group C')) :?>
    				C - <?php the_title();?><br />
    			<?php endif;?>
    			<?php if (in_category('First')) :?>
    				<?php the_title();?><br />
    			<?php endif;?>
    		<?php endif; ?>
    
        <?php endwhile; endif; ?>
    
        <?php $query->rewind_posts();?>
    
       <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    		<?php if ( in_category( array( 'First', 'Group A', 'Group B','Group C' ) ) ) continue; ?>
        		<?php the_title(); ?><br>
    		<?php endwhile; ?>
  • Ash

    Hi there

    Well, in that case, you have to run three different queries. Set argument like this:

    $args=array(
          'showposts' => -1,
          'category__in' => array('group_1'),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
    $args=array(
          'showposts' => -1,
          'category__in' => array('group_2'),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);
    $args=array(
          'showposts' => -1,
          'category__in' => array('group_3'),
          'caller_get_posts'=>1
        );
        $posts=get_posts($args);

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

  • Gregory

    Got it working. Next question is how do I surround those groups with a "div"? For example:

    <div class="group">
    Group A
    Group A
    </div>

    I would like to do that with each group. The code I'm using:

    <?php if ( $query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
    
        	<?php if ( in_category( 'Group A' ) ) : ?>
            	Group A - <?php the_title(); ?><br>
       		<?php endif; endwhile; ?>
    
        	<?php $query->rewind_posts();?>
    
        	<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
        	<?php if ( in_category( 'Group B' ) ) : ?>
            	Group B - <?php the_title(); ?><br>
       		<?php endif; endwhile; ?>
    
        	<?php $query->rewind_posts();?>
    
        	<?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
        	<?php if ( in_category( 'Group C' ) ) : ?>
            	Group C - <?php the_title(); ?><br>
       		<?php endif; endwhile; ?>
  • Ash

    In that case, try the following:

    <div class="group">
    <?php if ( $query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
    
    <?php if ( in_category( 'Group A' ) ) : ?>
            Group A - <?php the_title(); ?><br>
            <?php endif; endwhile; ?>
    
    <?php $query->rewind_posts();?>
    </div>
    <div class="group">
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
    <?php if ( in_category( 'Group B' ) ) : ?>
            Group B - <?php the_title(); ?><br>
            <?php endif; endwhile; ?>
    
    <?php $query->rewind_posts();?>
    </div>
    <div class="group">
    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
    
    <?php if ( in_category( 'Group C' ) ) : ?>
            Group C - <?php the_title(); ?><br>
            <?php endif; endwhile; ?>
    </div>

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.