[wp-hackers] Function to get posts by meta

Bill Dennen dennen at gmail.com
Tue Aug 3 13:11:45 UTC 2010


Try adding:

post_type=any

By default, it  pulls post_type=post

-Bill

On Tue, Aug 3, 2010 at 9:05 AM, Christian Gundersson <christian at buro2.se> wrote:
> Hi,
>
> thanks for the hints. Using the get_posts route gives me nothing in return
> however, so perhaps I'm using it wrong. This is what I did
>
> $posts = get_posts ( "meta_key=producer&meta_value=" . $producer_id );
>
> $posts turns out as an array that is empty. Have I misinterpreted the use of
> the function? The line sits inside a loop.
>
> Louy, I checked your link and will study it more in detail later, but it
> seems like a cumbersome approach for something quite simple. It'll probably
> make more sense when I get the time to read it properly :)
>
> Thanks again though!
>
> //Christian, Büro 2
>
>
>
>
>
>
>
> 2010/8/3 Bill Dennen <dennen at gmail.com>
>
>> Or this, which lets you specify a custom post type:
>>
>> http://codex.wordpress.org/Function_Reference/query_posts
>>
>> -Bill
>>
>> On Tue, Aug 3, 2010 at 8:08 AM, Bill Dennen <dennen at gmail.com> wrote:
>> > Christian,
>> >
>> > Could you use this?
>> >
>> > http://codex.wordpress.org/Template_Tags/get_posts
>> >
>> > $post_type
>> > (string) (optional) The type of post to show. Available options are:
>> > post - Default
>> > page
>> > attachment
>> > any - all post types
>> > Default: post
>> >
>> > $meta_key and $meta_value
>> > (string) (optional) Only show posts that contain a meta (custom) field
>> > with this key and value. Both parameters must be defined, or neither
>> > will work.
>> >
>> > On Tue, Aug 3, 2010 at 7:56 AM, Christian Gundersson <christian at buro2.se>
>> wrote:
>> >> Hi,
>> >>
>> >> I'm writing a plugin for a client using custom post types and ran across
>> a
>> >> situation where I wanted to retrieve post data from posts that shared
>> the
>> >> same meta data (key and value). I didn't find anything in Wordpress core
>> or
>> >> by searching so I wrote a function for it myself and I'm wondering if it
>> >> would be of any interest to anyone else.
>> >>
>> >> The function, as said, takes a meta key and a meta value as argument and
>> >> then performs a search for posts that has that exact match. The return
>> value
>> >> is an array with the post data.
>> >>
>> >> The code is viewable on github: http://gist.github.com/506224
>> >>
>> >> I have two questions on this though.
>> >>
>> >> 1) Have I done the function properly? (I'm fairly new so I'd like some
>> tips
>> >> and hints to improve my coding skills on Wordpress)
>> >>
>> >> 2) Would this be of any interest for inclusion in Wordpress? And if not,
>> why
>> >> not?
>> >>
>> >> Thanks!
>> >>
>> >> Regards,
>> >> //Christian, Büro 2
>> >> _______________________________________________
>> >> wp-hackers mailing list
>> >> wp-hackers at lists.automattic.com
>> >> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> >>
>> >
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list