WordPress.org

Ready to get started?Download WordPress

Forums

Using conditionals with custom fields (3 posts)

  1. Ryan
    Member
    Posted 6 years ago #

    Hey folks, have a bit of an advanced WordPress question. What I'd like to do is use a conditional to determine if a custom field has content (no problem), and then if it does, display the post, if it doesn't, move on to the next post (also, not too much of a problem) - the catch: I only want to display a single post.

    Anyone have any ideas how this might be done?

  2. nudnik
    Member
    Posted 6 years ago #

    Look at the code in the search-everything plugin. I think you'd need to write a custom SELECT query using whatever join it says for searching metadata, and check WHERE [certain custom field] != ''.

    The dirtier (but easier way) would be to have a query that selects the maximum number of recent posts that might not have that custom field set (i.e. 10), then loop through them. When you find one with the custom field set, then display that post and break out of the foreach.

    The latter could very easily break, though, and puts an unnecessary load on the database.

  3. Kafkaesqui

    Posted 6 years ago #

    Another search plugin option, though this one already works with custom fields:

    http://guff.szub.net/2006/04/21/search-custom-fields/

Topic Closed

This topic has been closed to new replies.

About this Topic