I have several custom posts that have a meta field that's just an integer, and that integer refers to another custom post and I need to get data from that integer.

For example, I have a custom post type called foods and it has a custom meta key of food_chart and a meta value of 20. 20 is the post ID of a custom post type called food_charts.

I want to be able to list and search/filter all the foods (can be easily done with meta queries), but I also want to add a search feature that lets the user filter by a value inside the food_charts set of meta values, like the food_charts author or time it was created. How would I filter search results from data in the food_charts post type while listing all the original food posts?

It's worth mentioning that I'm using Advanced Custom Fields Pro and the "recommended" ACF Search Plugin doesn't actually work for me, and I'm using WordPress 4.8.

  • I get the feeling you've gone terribly wrong somewhere in your data design if you need this. I don't think you can do this in a single query, but even if you could, it'd be hideously slow/expensive. Querying posts by their post meta is already one of the slowest things you can do in WordPress, I would expect the query to take several seconds to run. Have you considered a taxonomy? – Tom J Nowell Jul 6 '17 at 21:33
  • Really what you want here is a food chart taxonomy – Tom J Nowell Jul 6 '17 at 21:34
  • This taxonomy needs about 50 additional data points, though, and that's the trouble. – Kalob Taulien Jul 7 '17 at 1:52
  • You can map it on to a food_chat by making sure they have the same post slug, if you ever need to group posts so they can be found, or filtered, use a taxonomy. If post meta was fast enough they'd have used it to implement categories and tags, instead they added dedicated tables. Meta tables are optimised for fetching meta when you already know the post ID. Term tables are optimised for fetching the post IDs when you know the term to search by. Either way what you have is going to require multiple steps – Tom J Nowell Jul 7 '17 at 1:54
  • That's a good explanation, I'll give this a shot. Thanks @TomJNowell – Kalob Taulien Jul 7 '17 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.