2

NOTE: I've edited the code below to reflect a way that NOW WORKS, thanks for the help!

My query pulls all the data I'm asking for from my custom post and custom fields. The fields fill in and the rows and columns look good. But the css table is breaking such that (as Nathan points out below) each post shows in a separate table and I get a header over a row, header over a row, etc.

I've tried a number of ways to code the table, but the rows display exactly like that each time.

It's messy, I admit, But I am not taking any shortcuts on the table structure so to show exactly the way I am getting it to at least show all the data in the correct columns

<?php
 $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'event'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";

 $pageposts = $wpdb->get_results($querystr, OBJECT_K);

 ?>
 <div class="post" id="post-<?php the_ID(); ?>">
   <?php if ($pageposts): ?>
 <?php global $post; ?>
<table class="maee-table"><col width="20%"><col width="30%"><col width="20%"><col width="30%">
<tr><th class="column-head">Date</th>
<th class="column-head">Name</th>
<th class="column-head"> Audience</th>
<th class="column-head">Submitting partner</th>
</tr>
 <?php foreach ($pageposts as $post): ?>
 <?php setup_postdata($post); ?>

   <tr>
     <td class="maee-row"><?php rwmb_the_value( 'prefix-event_date1');?></td> 
 <td class="maee-row"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></td>
      <td class="maee-row" ><?php rwmb_the_value( 'prefix-event_audience1');?></td>
<td class="maee-row"> <?php the_category(', ') ?></td>
   </tr>
  <?php endforeach; ?>
  </table>
 </div>
 <?php endif; ?>
</section>
MARWEL58
  • 19
  • 1
  • 5
  • You aren't displaying your results in a single table. You're creating a new table for each post. – Nathan Dawson Aug 10 '19 at 01:25
  • yes, that's exactly the problem. Maybe I should I rephrase my title Question: How do I fix this so that it makes a single table? – MARWEL58 Aug 10 '19 at 03:35
  • Move the line that echo's the table to _before_ the `foreach` loop (and the line that echo's the closing table tag to _after_ the `endforeach`).... – random_user_name Aug 10 '19 at 03:43
  • Thanks for the response. I gave it a try but the result is the same. I tried a few other configurations (moving the whole header grouping to that position , for instance). Still the same result. – MARWEL58 Aug 10 '19 at 04:30
  • Please update the question with the modifications you made to the code so we can see what's been changed. – cabrerahector Aug 10 '19 at 04:32
  • This works now. I changed cale_b's suggestion and moved the ```foreach``` under the table and the header. Corrections are shown above). This only works with the tables hard coded -when css was echo'd, it would only pull in Wordpress data (title and category) Not the custom fields. – MARWEL58 Aug 11 '19 at 20:43

1 Answers1

0

You could try something like this:

    global $wpdb;

    $querystr = "
    SELECT $wpdb->posts.* 
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
    AND $wpdb->posts.post_status = 'publish' 
    AND $wpdb->posts.post_type = 'event'
    AND $wpdb->posts.post_date < NOW()
    ORDER BY $wpdb->posts.post_date DESC
 ";



    $content = '<table class="maee-table"><col width="20%"><col width="30%"> 
<col width="20%"><col width="30%">';

    $content .= '<tr><th>Date</th><th>Name</th><th>Audience</th><th>Submitting Partner</th></tr>';

    $results = $wpdb->get_results($querystr);

    global $post; // left this out before.  Try this.    

    foreach ($results as $post) {

        setup_postdata($post);

        $content .= '<tr>';
        $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
event_date1') . '</td>';
        $content .= '<td class="maee-row">' . the_title() . '</td>';
        $content .= '<td class="maee-row">' . rwmb_the_value( 'prefix- 
event_audience1') . '</td>';
        $content .= '<td class="maee-row">' . the_category(', ') . '</td>';
        $content .= '</tr>';
    }
    $content .= '</table>';

    // return the table
    echo $content;
DubVader
  • 1,032
  • 1
  • 6
  • 8
  • This is clearly more elegant, so thank you! It is creating just one table, but it isn't pulling in any data. The code I was using pulled in everything. But I will play around this. And thanks again! – MARWEL58 Aug 11 '19 at 01:09
  • I left out the global $post in my example, I've added it in. That's important for the setup_postdata, so maybe give that at try. Could be why your example was getting data and this one was not. – DubVader Aug 12 '19 at 13:27