Note that there are some explanatory texts on larger screens.

plurals
  1. POCan WordPress WP_Query return custom field post meta?
    text
    copied!<p>For some reason, I cannot get WP_Query to return the custom field values of posts. I can get the post thumbnails using <code>get_the_post_thumbnail($post-&gt;ID, array(50,50))</code>, but I cannot get the custom field data using <code>get_post_meta($post-&gt;ID, $key, true)</code>.</p> <p>A stripped-down version of what I'm trying to do:</p> <pre><code>&lt;?php $keys = array('Show Date','Birth Year','Origin'); echo '&lt;table&gt;'; echo '&lt;tr&gt;&lt;th&gt;Title&lt;/th&gt;'; foreach( $keys as $key ) { echo '&lt;th&gt;' . $key . '&lt;th&gt;'; } echo '&lt;/tr&gt;'; $myquery = new WP_Query( 'post_type=post' ); if( $myquery-&gt;have_posts() ) : while( $myquery-&gt;have_posts() ) : $myquery-&gt;the_post(); $title = get_the_title(); echo '&lt;tr&gt;&lt;td&gt;' . $title . '&lt;/td&gt;'; $values = array(); foreach( $keys as $key ) { $values[] = get_post_meta($post-&gt;ID, $key, true); } foreach( $values as $value ) { echo '&lt;td&gt;'; echo $value; echo '&lt;/td&gt;'; } echo '&lt;/tr&gt;'; endwhile; endif; echo '&lt;/table&gt;'; ?&gt; </code></pre> <p>Also available here:<br> <a href="http://pastebin.com/at8S2THs" rel="nofollow">http://pastebin.com/at8S2THs</a></p> <p>Even with all non-essential code removed, I cannot make this work. I know you can use parameters like <code>meta_key</code> and <code>meta_value</code> in a query to narrow down the results, but I just want to display all values for the keys I specify, if they exist, for each post.</p> <p>Any help would be greatly appreciated...</p> <p>** SOLUTION FOUND **</p> <p>Just needed to add <code>global $post;</code> after the start of the loop. Thanks to @Kimikaze on the WP support forum for providing the solution!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload