Store posts in an array?

Hi again,

What's the best way to store posts in a variable and be able to use wordpress functions outside the loop?

This is what my query looks like:

<?php $args = array('tax_query' => array(array('taxonomy' => 'post-status','field' => 'slug','terms' => array ('post-status-published'))));$query = new WP_Query( $args ); $groupa = array(); $groupb = array(); $groupc = array(); $groupd = array(); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

<?php endwhile; wp_reset_postdata(); endif; ?>

I was thinking of doing something like "if(has_category('group-a')) = $groupa" inside the query and then use $groupa outside the loop. Also, how do I use wordpress functions like thumbnail(),the_content(),the_title(),etc. outside the loop?

Thanks again,

  • Milan
    • WordPress Wizard

    Hello Gregory ,

    Hope you are well today and thanks for asking us. :slight_smile:

    If you want to search posts which are in specific category, better way would be of using this statement.

    $query = new WP_Query( array( 'category_name' => 'staff' ) );

    $query will hold, posts of "staff" category. You can then fire loop to loop through all the posts. :slight_smile:

    Best Regards,
    Milan

  • Gregory
    • Site Builder, Child of Zeus

    Didn't do anything.

    <?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 $groupa = new WP_Query( array( 'category_name' => 'group-a' ) );?>
    
    	<?php if ($groupa) :?>
    		group a - <?php the_title();?><br>
    	<?php endif;?>
    
    <?php endwhile; wp_reset_postdata(); endif; ?>

    All it did was add 'group a' in front of every post.

  • Milan
    • WordPress Wizard

    Hello Gregory ,

    Hope you are doing well. :slight_smile:

    I didn't say you to add the statement inside the loop. Actually my point was to you that you use it like this way,

    $args = array(
    			'category_name' => 'group-a',
    			'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 the_title(); ?>
    <?php endwhile; wp_reset_postdata(); endif; ?>

    This will fetch all the posts of group-a and display its title. :slight_smile:

    Kind Regards,
    Milan

  • Gregory
    • Site Builder, Child of Zeus

    I apologize but unfortunately I'm doing a complex query with multiple loops based on the categories in the loop where storing posts in a '$variable' is needed, mainly for <divs> and combing items like if only group-a and another loop if query contains group-a and group-b and another loop that doesn't have show posts in the first loop but split into two columns. I can't write in words why it's need because I've spend so many days figuring this out that I can't explain what I need in words and I'm frustrated and exhausted trying to figure this out.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.