I want to get all the data with the meta key of "project_id" from post meta table. I wrote a query, it should work in thoery, but I have no way to see what I get, and inject this query to my plugin doesn't generate anything. Please take a look at this query, and help me understand if it is correct, what's the formate of the outcome, How can I echo it to see what I get:

function mp_all_ids( ){
global $bp, $wpdb, $post;

$query = <<<QUERY
SELECT      project_id.post_id
FROM        $wpdb->postmeta project_id
LEFT JOIN  $wpdb->postmeta height 
            on height.post_id = project_id.post_id
            and height.meta_key = 'height'
LEFT JOIN  $wpdb->postmeta width 
            on width.post_id = project_id.post_id
            and width.meta_key = 'width' 
WHERE       project_id.meta_key = 'project_id'
ORDER BY    project_id.meta_value+(0) ASC
$data = $wpdb->get_results( $query );

return apply_filters( 'mp_ids', $data );

Edit: the var_dump gets the following result:

array(3) {
  object(stdClass)#457 (1) {
    string(3) "385"
  object(stdClass)#458 (1) {
    string(3) "451"
  object(stdClass)#377 (1) {
    string(3) "453"

How can I access the 3 strings in each object?

2 Answers 2


First of all, you should look into using WP_Query for this:


Secondly, to see the results, just do a var_dump() before the return:

echo '<pre>';
var_dump( $data );
  • Thanks, @scribu! var_dump prints the result. Seems the query somewhat correct. Would you please take a look at the outcome(I'll pate it bellow the original question)and tell how to access the 3 strings in each object?
    – Jenny
    Commented May 30, 2011 at 1:04
  • @scribu, thanks for the link to the query_post turorial. I might need that in later steps. For now, I need to query the object so that I can create a loop according to the ids, then, to display the objects on posts. please help!
    – Jenny
    Commented May 30, 2011 at 1:20
  • You can use get_posts() instead of query_posts(). Not gonna help with the direct query. It's for your own good. :P
    – scribu
    Commented May 30, 2011 at 2:00
  • By the way, these are not really WordPress questions, but rather general PHP questions. You should read a couple of basic PHP tutorials before posting here.
    – scribu
    Commented May 30, 2011 at 2:03
  • 2
    I guess the problem is that we're not trying to be KFC here. (That would be the wp.org support forums.) Don't know if we're succeeding. :-|
    – scribu
    Commented May 30, 2011 at 13:31

To test if the query is working you could output the $query string that you have created and manually running that query on the wordpress database via PhpMyAdmin or a similar interface.

If there is any errors in the query or the query returns no results than you will be able to tweak it to get your desired effect before putting it in your code.

  • yes, how to output the $query string?
    – Jenny
    Commented May 30, 2011 at 0:34

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.