0

I needed to display some data in a simple html table so I started with very basics of table. As shown in the code, the data needed was a list of custom-field-values of a meta_key submitdate, from across all the posts, under a specific tag "female". I wrote the following code and everything worked perfectly till the first WP_Query.

UPDATE : Current code as suggested by @islam.

<table border="1">
<thead>
<tr>
<th>NO.</th>
<th>FEMALE DATA</th>
<th>MALE DATA</th>
</tr>
</thead>
<tbody>


<?php
$fmargs = array ( 'tag' => 'female', 'posts_per_page' => -1 );
$female_query = new WP_Query( $fmargs );
while ( $female_query -> have_posts() ):
$female_query -> the_post(); ?>
<tr>
<td></td>
<td>
<?php  echo get_post_meta($post->ID, 'submitdate', true); ?>
</td>
<?php endwhile;
wp_reset_postdata(); //reset the first query
?>


<?php
$margs = array ( 'tag' => 'male', 'posts_per_page' => -1 );
$male_query = new WP_Query( $margs );
while ( $male_query -> have_posts() ):
$male_query -> the_post(); ?>
<td>
<?php echo get_post_meta($post->ID, 'submitdate', true);?>
</td>
</tr>
<?php endwhile;
wp_reset_postdata(); //reset the second query
?>



</tbody>
</table>

Now, I needed a list of data filled in similar way into second column in the same table for another tag "male". When I tried a second similar WP_Query, it returns the data but it either fills up into the wrong row or a single cell or completely breaks out of table. as I tried endless varieties of <tr> and <td> . So, I am unable to get the data listed into the second column exactly as the first column.

After writing so far I just realized that this question could be categorized as non-wordpress issue, so if you feel I should definitely ask it somewhere, Please let me know, else a little helping hand is always appreciated. Here is a screenshot of the current result. Please help. enter image description here

2 Answers 2

2

Here is what is happening with your code:

  1. query_posts uses the global variable wp_query. It always overwrites that variable, which is why you should not use query_posts pretty much ever.
  2. Your first query_posts clobbers the original $wp_query data.
  3. You start the Loop
  4. 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.
  5. You then clobber $wp_query yet again
  6. And start a new loop
  7. 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.

7
  • hope the updated code in accordance to your suggestion.
    – gurung
    Commented Sep 29, 2013 at 17:56
  • That is the idea, but you have now edited the question such that it is not the same question anymore.
    – s_ha_dum
    Commented Sep 29, 2013 at 17:58
  • I will edit the question/answer in order to make sense. Problem is still I cant get the data (list) of second query into the second column properly as it does on first column. Any ideas if I am placing the <td> or <tr> wrong, or is it not possible ?
    – gurung
    Commented Sep 29, 2013 at 18:02
  • ,new code is flawless and it inserts post titles to table cells now, instead can it be made to return the values of the custom field please. I tried get_post_meta($post->ID, 'submitdate', true) and it didnot work.
    – gurung
    Commented Sep 29, 2013 at 20:23
  • @gurung : see the edit
    – s_ha_dum
    Commented Sep 29, 2013 at 20:48
1

First of all DO NOT use query_posts(), it will modify your original loop; use WP_Query() instead (Why?). And you can call them as of your need (See here).

<?php //Female Query ?>
<?php
$fmargs = array ( 'tag' => 'female', 'posts_per_page' => -1 );
$female_query = new WP_Query( $fmargs );
while ( $female_query -> have_posts() ):
$female_query -> the_post();
   echo get_post_meta($post->ID, 'submitdate', true);
endwhile;
wp_reset_postdata(); //reset the first query
?>

And

<?php //Male Query ?>
<?php
$margs = array ( 'tag' => 'male', 'posts_per_page' => -1 );
$male_query = new WP_Query( $margs );
while ( $male_query -> have_posts() ):
$male_query -> the_post();
   echo get_post_meta($post->ID, 'submitdate', true);
endwhile;
wp_reset_postdata(); //reset the second query
?>
1
  • thnks, code updated, the table issue persists though.
    – gurung
    Commented Sep 29, 2013 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.