• Hi There,

    I have this query, and I need to switch it up a bit to set up the post data. Right now it only pulls the post ID. Looking for suggestions on how to do that.

    <?php
    		  $query = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'grade-type' AND meta_value like '%Post Secondary%'";
    		  $rows = $wpdb->get_results($query);
    		  foreach($rows as $row){
    		    echo "the id post id: " . $row->post_id;
    		    echo "<br />";
    		    echo $row->post_title; 
    
    		  }
    		?>

    Thanks!

    Nadine

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter nadine00

    (@nadine00)

    Are you talking about everything from

    if ($postids) {….

    down in that example or the whole thing? Is there a way I can set up post data without changing the query line?

    Sorry, for the questions, its probably right in front of me….my brain is rebelling this morning.

    you could try to use ‘setup_postdata()’:

    foreach($rows as $row){
          setup_postdata($row);
          echo "the id post id: " . $row->post_id;
          echo "";
          echo $row->post_title; 
    
    		  }
    Thread Starter nadine00

    (@nadine00)

    Hmm. That doesn’t echo the post_title. It still only echos the post ID only.

    I was also trying something like this, from the general query example, but it comes out blank:

    <?php
    		$query = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'grade-type' AND meta_value like '%Post Secondary%'";
    		$rows = $wpdb->get_results($query);
    		if ($rows) :
    			foreach ($rows as $post) :
    				setup_postdata($post);?>
    
    			<h2><a href="<?php the_permalink(); ?>" rel="bookmark"
    				title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
    		<?php
    			endforeach;
    		else :
    		?>
    		<h2> Not Found</h2>
    		<?php endif; ?>
    Thread Starter nadine00

    (@nadine00)

    ok trying this:

    <?php
    		$querystr = "SELECT *
    		FROM $wpdb->postmeta, $wpdb->posts
    		WHERE meta_key = 'grade-type'
    		AND meta_value like '%Post Secondary%'
    		ORDER BY wposts.post_date DESC
    		";
    
    		$pageposts = $wpdb->get_results($querystr, OBJECT);
    
    		?>
    
    		<?php if ($pageposts): ?>
    		  <?php foreach ($pageposts as $post): ?>
    		    <?php setup_postdata($post); ?>
    
    		    <h4><?php the_title(); ?></h4>
    
    		    <?php endforeach; ?>
    		    <?php else : ?>
    		        <h2 class="center">There seems to be a glitch here.</h2>
    
    		     <?php endif; ?>

    I am not a programmer, but I am trying. Any suggestions? I am not sure about the above link’s example about joining.

    thanks

    Nadine

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Setting up post data’ is closed to new replies.