Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
QUERY;
$data = $wpdb->get_results( $query );

return apply_filters( 'mp_ids', $data );
}

Edit: the var_dump gets the following result:

array(3) {
  [0]=>
  object(stdClass)#457 (1) {
    ["post_id"]=>
    string(3) "385"
  }
  [1]=>
  object(stdClass)#458 (1) {
    ["post_id"]=>
    string(3) "451"
  }
  [2]=>
  object(stdClass)#377 (1) {
    ["post_id"]=>
    string(3) "453"
  }
}

How can I access the 3 strings in each object?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

http://scribu.net/wordpress/advanced-metadata-queries.html

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

echo '<pre>';
var_dump( $data );
share|improve this answer
    
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 May 30 '11 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 May 30 '11 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 May 30 '11 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 May 30 '11 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 May 30 '11 at 13:31
show 4 more comments

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.

share|improve this answer
    
yes, how to output the $query string? –  Jenny May 30 '11 at 0:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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