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 have some post ids in an ID array, like:

$ids = array(20,34,65,126) ;

There is many custom post types in my project, these ids are related to different post types, but I dont know which id is related to which post type in that condition. In another word, I need to do something like this:

$posts = my_get_posts(
   'ids' => array(20,34,65,126),
   'post_type' => 'product',
) ;

That function has to be eliminate ids which is not a producy.

How to do this?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I am assuming this-- "That function has to be eliminate ids which is not a producy"-- means "I only want to pull the post associated with the ID if that post is a product". In that case a simple query should do it:

$pqry = new WP_Query(
  array(
    'fields' => 'ids', // if you only want the Post IDs
    'post_type' => 'product',
    'post__in' => array(20,34,65,126)
  )
);

Reference:

http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer
    
That was very short and complex usage like a tequila shot, Thank you! –  Digerkam Feb 25 '13 at 16:01
    
Do I have to use WP_Query? "get_posts" seems not work –  Digerkam Feb 25 '13 at 18:01
    
I prefer WP_Query. get_posts should probably work but the syntax may be different. I'd have to look it up to be sure, since I virtually never use it. –  s_ha_dum Feb 25 '13 at 18:04
    
It worked then, but the result is weird, it returns the array of ids which from selected post type, not posts :) –  Digerkam Feb 25 '13 at 18:48
    
Is that not what you asked for? –  s_ha_dum Feb 25 '13 at 19:38

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.