Here is what is happening with your code:
query_posts
uses theglobal
variablewp_query
. It always
overwrites that variable, which is why you should not use
query_posts
pretty much ever.- Your first
query_posts
clobbers the original$wp_query
data. - You start the Loop
- The first thing you do in that loop is reset
$wp_query
to the
original query. Now the$wp_query
global
has different data that
it had when the loop started. - You then clobber
$wp_query
yet again - And start a new loop
- And then reset the query again.
In other words, your data is overwritten and out of sync is several different ways. The fix is to not use query_posts
. Use a new WP_Query
object instead.
$outer = new WP_Query( array ( 'tag' => 'female', 'posts_per_page' => -1 ) );
if ($outer->have_posts()) {
while ( $outer->have_posts() ) {
$outer->the_post();
// ...
The rest of your problem is really a PHP/HTML one, but here is a rough outline. What you need to do is iterate over both arrays at once.
$args = array ( 'post_type' => 'post', 'posts_per_page' => -1 );
$female_query = new WP_Query( $args );
$args = array ( 'post_type' => 'page', 'posts_per_page' => -1 );
$male_query = new WP_Query( $args );
$count = max($female_query->found_posts,$male_query->found_posts);
var_dump($count);
echo '<table>';
for ($i = 0; $i < $count; $i++) {
echo '<tr>';
echo '<td>';
echo $i;
echo '<td>';
echo '<td>';
if (isset($female_query->posts[$i])) {
echo $female_query->posts[$i]->post_title;
}
echo '<td>';
echo '<td>';
if (isset($male_query->posts[$i])) {
echo $male_query->posts[$i]->post_title;
}
echo '<td>';
echo '</tr>';
}
echo '</table>';
$post
isn’t set in my code. This is a non-standard Loop because you need data from two queries at once. To get the meta data use $female_query->posts[$i]->ID
instead of $post->ID
or pass it through setup_postdata
— setup_postdata($female_query->posts[$i])
— though that is probably overkill for this circumstance.