WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to get all the post ids when the post meta includes a key of "project_id". This function get the job down, but need refine:

function get_all_ids(){
global $wpdb, $post;
$meta_key1 = 'project_id';
$postids = $wpdb->get_col( $wpdb->prepare( "
    SELECT key1.post_id
    FROM $wpdb->postmeta key1
    WHERE key1.meta_key = %s",$meta_key1 ) );
return $postids;

This returns arrays of (0 =>1), ( 1 =>3 ), (2 =>5) In the later step, I will need to get the ids like this:

$this->results = $projects->get_all_ids();

This requires the ids are not arrays. How can I get all the ids without array?

share|improve this question
In what format do you expect to have the list of ids? – MZAweb May 29 '11 at 6:49
I'm not sure. I am trying to make a loop for an object which attached to each post. If has ids, the loop will start. When there is an id, I can get data from a class. – Jenny May 29 '11 at 6:58
If there is a loop that process those ids then array is the best format, but if you can show us that loop then we will actually know in what format does the loop expects the result. – Hameedullah Khan May 29 '11 at 7:12
the loop is too long, this comment box not allow. – Jenny May 29 '11 at 7:53
ok, find a way-- this is the original loop: github.com/kunalb/EventPress/blob/master/views/kb-loop.php , I will make an extends of this kb-loop for my object. – Jenny May 29 '11 at 8:01

If you need the ids separated by coma you can get them from the array you have:

$listids = implode ( "," , $postids );

In any case, please provide more info or code about what you need

share|improve this answer
would you please take a look at the loop I posted in the above comment? – Jenny May 29 '11 at 22:30
I want the formate llike this: 'project id' => 1,'height'=>x,'width'=>y – Jenny May 29 '11 at 22:44

Your Answer


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.