0

Have this code. In doing a var_dump the meta_value is being retrieved under public object meta_value. I'm trying to access this data in a "WordPress" away instead of a more traditional PHP way.

What is the most efficient/best practice way of getting this meta value data?

The end goal is to get an array like:

array(
    [0]=>array("post_title"=>"title of post", "img_url"=>"url.com")
)

Thanks!

global $post;
//Get all products, id, name, and thumbnail image
$args = array(
    'post_type' => 'ral-profile',
    'posts_per_page' => -1,
    'orderby' => 'rand',
    'meta_query' => array(
        array( 'key' => '_thumbnail_id')
    )
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        //post actions
    }
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
  • LIke get_post_meta() ? – Howdy_McGee Jun 25 '15 at 14:53
  • I'm not sure if get_post_meta() will requery the DB. Since it's already in the wp_query result, I just need to access that result. – W00tW00t111 Jun 25 '15 at 14:55
  • Post Meta isn't queried in the WP_Query. WP_Query queries the wp_posts table and post meta is stored in the wp_postmeta table. You have to make a secondary call to get post meta which is what the get_post_meta() function is for. – Howdy_McGee Jun 25 '15 at 14:56
  • So 'meta_query' => array( array( 'key' => '_thumbnail_id')) in the wp_query args is not included in the query? – W00tW00t111 Jun 25 '15 at 14:59
2

The WP_Query() only queries posts from the wp_posts table. Even if you add additional parameters such as:

'meta_query' => array( 
    array( 
        'key'     => '_thumbnail_id'
        'compare' => 'EXISTS'
    )
)

It looks at the wp_postmeta table to ensure that the posts it pulls has the post_meta _thumbnail_id but it does not also pull the post meta. The query only pulls post data from the wp_posts table. WordPress has a ton of useful functions to get ahold of this information though:

  • the_post_thumbnail() pulls and displays the post thumbnail in an <img/> tag.
  • get_the_post_thumbnail() - Still pulls the post thumbnail in an <img/> tag but does not display it. Allows you to store it into a variable for later use.

To achieve what you're trying to do we first have to get the ID of the post-thumbnail, then get the URL:

$thumb_id        = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src( $thumb_id, 'medium', true );
$thumb_url       = $thumb_url_array[0];

You can switch out medium with whatever size you need for the post thumbnail.


Alternatively, you could use get_post_meta() to get the thumbnail ID ( or any other post meta values ):

$thumb_id        = get_post_meta( $post->ID, '_thumbnail_id', true );
$thumb_url_array = wp_get_attachment_image_src( $thumb_id, 'medium', true );
$thumb_url       = $thumb_url_array[0];
| improve this answer | |
  • Phew! So use to straight PHP and frameworks - am getting use to the WP way of doing things. Thank you for the help! – W00tW00t111 Jun 25 '15 at 15:20
  • No problem! You can view the entire Query Object by printing it out: print_r( $the_query ); but note that only parameters in the posts index can be accessed in "The Loop's" $post object. The other indexes are explained via Methods & Properities in the Codex. – Howdy_McGee Jun 25 '15 at 15:27
  • Sweet that link is exactly what I was looking for! I've just been going through the github repo to understand what is going on b/c sometimes the codex is a bit lacking - but that is super helpful. Thanks again! – W00tW00t111 Jun 25 '15 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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