0

This question might have an answer already in the forums, however I still wanted to ask it as this is a different more complicated version of the problem.

The story:

We have 2 meta fields for posts:

  • category_sticky
  • category_featured

Each of them allows editors to input a list of categories for which the post will be featured and sticky respectively.

In the database I store this as meta fields category_sticky and category_featured:

database sample

I would like to make a query - if possible using wp_query - to get posts for a category ordered by category_featured and then by category_sticky.

Or if you have better suggestions regarding implementation?

  • I think that you can't use WP_Query but neither any custom SQL query. Once meta value contains string list of categories, and not a single value, you need PHP postprocessing to order the posts. It would be more easy and efficient if you had stored values as category meta, and not post meta, but I guess now it's too late for that... – gmazzap Oct 9 '14 at 10:01
  • Okay what if I would store each meta value in it's own field -e.g. have multiple meta values for category_sticky and category_featured? – adrian7 Oct 9 '14 at 10:03
  • In that case a custom SQL query can probably do the trick, but it would be all but efficient, and propably PHP ordering is quicker. But can't be sure without testing. – gmazzap Oct 9 '14 at 10:14
  • If you have few hundred thousands posts I would like to let MySQL do the ordering – adrian7 Oct 9 '14 at 10:26
  • Consider that if you store meta value with single category id - that is required to order things with MySql - meta values rows for this meta key becomes about 4x or 5x number of posts. So, I for myself, had tried both, make some tests, than choose the faster, because I'm not sure that an SQL query like that is faster than a PHP routine and the only way to know is testing because it depends by different things, e.g. how fast is DB server? is load balanced? is cached? which type of cache? which version of PHP are you using? which web server? ... – gmazzap Oct 9 '14 at 10:44

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.