Reputation: 391
I have a custom post type with a couple of custom post fields. What I am trying to do is to display field 1 of all posts, then put some html content, and then display field 2 for all posts. So, the structure will be something like this:
- Field 1 for post 1
- Field 1 for post 2
- Field 1 for post 3
- Field 1 for post 4
HTML CONTETENT GOES HERE
- Field 2 for post 1
- Field 2 for post 2
- Field 2 for post 3
- Field 2 for post 4
Is there a better way to do it than to query the posts twice? It's the exact same set of posts.
Currently my Post query and loop looks like this
// Query to display Field 1 for all posts
<?php
$new_loop = new WP_Query( array(
'post_type' => 'pbt',
'posts_per_page' => 5
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
FIELD 1 DATA GOES HERE
<?php endwhile; else: endif; ?>
<?php wp_reset_query(); ?>
MY HTML CONTENT GOES HERE
// Query to display Field 2 of posts
<?php
$new_loop = new WP_Query( array(
'post_type' => 'pbt',
'posts_per_page' => 5
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
FIELD 2 DATA GOES HERE
<?php endwhile; else: endif; ?>
<?php wp_reset_query(); ?>
Is there a cleaner way to do it? As you can see, I'm repeating the query and post loop twice in order to put html data between them.
Is there a way I could pull field 1 for all posts, then pause the loop or something, put html content, and then pull field 2 for all posts without repeating the query?
Upvotes: 0
Views: 783
Reputation: 1432
// Query to display Field 1 for all posts
<?php
$new_loop = new WP_Query( array(
'post_type' => 'pbt',
'posts_per_page' => 5
) );
?>
<?php if ( $new_loop->have_posts() ) : while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
FIELD 1 DATA GOES HERE
<?php endwhile; else: ?>
MY HTML CONTENT GOES HERE
// Query to display Field 2 of posts
<?php while ( $new_loop->have_posts() ) : $new_loop->the_post(); ?>
FIELD 2 DATA GOES HERE
<?php endwhile; else: endif; ?>
<?php wp_reset_query(); ?>
Upvotes: 0
Reputation: 546
@shtarley
May be it is a better way to make two arrays from one loop i.e.
$field1[]; $field2[];
Store all the values from wp_query loop inside this two arrays and after that just show first array and then your html and after that second array.I don't know but i am assuming that you need both fields on different different palces.
Upvotes: 1