Support » Developing with WordPress » Using conditionals with custom fields

  • Ryan


    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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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.

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using conditionals with custom fields’ is closed to new replies.