post-page

How to Only Retrieve Posts With Custom Fields

32
responses
heading
heading
heading
32
Responses

 

Comments

  1. John Kolbert (23 comments.) says:

    Ronald, excellent post! I haven’t had a chance to test it yet, but I look forword to giving it a try.

  2. Ronald Huereca (66 comments.) says:

    @John,
    Good luck with it. Hopefully it works out for you.

  3. Jacob Santos says:

    I bow to you and your l33t mastery of WordPress Loop. I would totally do a highlander reference, but I’ll have to make sure that everyone gets it. So I’m not, but yeah “There can only be one!”

    I do like the current set of articles, but think that they should also be in the Codex.

  4. Ronald Huereca (66 comments.) says:

    @Jacob,
    By all means, go for it :) – the Codex that is, not the highlander reference.

  5. Matt Goff (1 comments.) says:

    Thanks for writing this article, it seems very close to something I would like to be able to do, except with pages. Do you know of a relatively easy way to get this to work with pages instead of posts?

  6. Ronald Huereca (66 comments.) says:

    @Matt,

    No, I haven’t played around with pages a whole lot. Perhaps you can try tapping into the ‘posts_where’ filter if you want to limit the results further.

    Can anyone else help Matt with this?

  7. DFOE says:

    very nice! it would also be handy to have something like this that matched key/value pairs (where x=y in custom fields).

  8. Viper007Bond (91 comments.) says:

    Pages are just posts with a different post_type.

    I haven’t tried it, but this _may_ work (I’ve only glanced at query.php):

    $customPosts->query('showposts=5&post_type=page');

  9. Ekro says:

    This only lists posts with defined custom fields. Is there any way to still append posts without custom fields? And does paging work with this solution? Thanks!

  10. Gordon Brander (2 comments.) says:

    Thanks for the tip! Very helpful. I was only aware of the_meta() template tag which isn’t nearly as flexible as get_post_custom_values().

  11. Ronald Huereca (66 comments.) says:

    @Viper,
    Thanks for weighing in.

    @Ekro,
    This technique only returns posts just with custom fields. Paging doesn’t currently work. I’ll probably write a separate post on that.

  12. Dan (5 comments.) says:

    Great post! Thanks! I’d done something similar but basically used a query object and pulled everything out that way. Is this way more efficient? On my personal site I’ve implemented something using your mechanism, but I’m wondering if it’s worth updating my other project…this way is slightly cleaner I think. Very interested to know if it’s more efficient. Thanks!

  13. Kang Oedin (1 comments.) says:

    Hi…
    You have many excellent articles, tutorials and posts about WordPress. Can I translate them to my native language (Bahasa Indonesia) and place them on my blog leaving the original link to you???

    Thank you very much…

  14. Ronald Huereca (66 comments.) says:

    @Kang,
    Please get in contact with Mark via the WLTC contact form. He’ll be able to answer your question.

  15. Ronald Huereca (66 comments.) says:

    @Dan,
    This way is perhaps more reusable. More efficient? Not likely. If anything, I’d say your technique and this one are about the same. Both make use of one query. The only difference is, I modify the query for posts using built-in WordPress filters. You probably call a query directly.

  16. Maria says:

    Thank you so much for this post! I was trying to figure it out myself, but that didn’t work.
    @Viper: that works!

  17. Marie-Aude (2 comments.) says:

    Thanks a lot, I was just trying to look for something like that.
    The only question – I’m really far from being an expert in coding – how can you then order the fields according to a custom field ?

  18. vnvsven (2 comments.) says:

    Great how-to !

  19. vnvsven (2 comments.) says:

    Is it possible to use this code also for pages with custom fields?

  20. Epic Alex (18 comments.) says:

    @Ronald – Thanks for the tutorial, its helping me to get to grips with something I’m trying to do, but I’m struggling a little. I’d like to display a list of posts where a certain custom field key has a certain value. So for example, show all posts where the key Number was equal to 10.

    I can’t work out if I should change the mysql query, or if i should alter something later on like use an if statement to check for a key equaling a value.

    Any help would be appreciated,
    Alex

  21. Tim says:

    Hi, I am not a PHP coder and not any idea about it. I have a simple and only one question. Can you please tell me how would I get to know which “KEY” have been assigned on a theme for the custom fields? All themes having different KEY. Most of them “Thumbnail” or “thumbnail” or “image etc. If there is a read me file for the theme then easy to get that. But if they don’t have a readme file it’s very difficult to get the KEY for the custom field. Can you please guide me how and where I can check which KEY the coder has been assigned or coded for the Custom Fields. Again I am not good in PHP. Please email me the detail. Thanks, Tim

  22. wza (1 comments.) says:

    excellent post, very helpful, thank you.

    i’d love to use the same method for my rss feed, as in only let posts thru that have a specific custom field. i’m trying to write a plugin that does this but i’m not there yet. any suggestions on that?

  23. ronggur (1 comments.) says:

    thanks Ronald this article work good for me.. i also apply another filter ‘posts_where’ but apparently by using this code the pagination is not working.. they won’t outside the loop. Do you know why this happen?

  24. vestmar (1 comments.) says:

    Hei Ronald,

    I am trying to adapt your code to my need (displaying a certain custom filed from all the posts within a give category) and I am already halfway:

    How can I instruct the code to display the custom fields of categeory ID=50 for example?

    After I will be able to “read” the custom fields form all the posts in cat 50, I wil want to strip all the caracters and have only the numbers, and then do an average with them (ie. the average price (my custom field is a price) in cat 50 is x).

    Any thougts about adapting for a certain category?

    Many thanks :)

    Puiu D. – Romania

  25. testdlir (1 comments.) says:

    tnx for this post…

    and i have a qustion. if i ant to list my post that has no any custom field, what i do?



Trackbacks/Pingbacks

  1. […] How to Only Retrieve Posts With Custom Fields (tags: wordpress) […]

  2. […] Menampilkan Posting Berdasarkan Custom Field Tertentu Posted by Kang Oedin April 17, 2008 Translated from How to Only Retrieve Posts With Custom Fields […]

  3. […] ici, on vous propose de créer une boucle WordPress entièrement grâce aux Query Posts et de récupérer des articles via les champs personnalisés de WordPress, les fameux “Custom […]

  4. […] How to Only Retrieve Posts With Custom Fields | Weblog Tools Collection One question I come across a lot regarding custom fields is how to only retrieve posts based on a custom field. […]

  5. […] How to Only Retrieve Posts With Custom Fields […]

  6. […] you might want to subscribe to the RSS feed for updates on this topic.So, you’ve read all the tutorials on how to get custom field values and how to get posts that have a certain key/value, but […]

  7. […] two articles using custom loops. The first was about how to create a custom loop. The second was how to retrieve posts based on custom fields. In both articles, several readers commented that they would like to see paging and have it […]

Obviously Powered by WordPress. © 2003-2013

page counter
css.php