0

Is it possible to sort posts by custom column in wp_posts table (in this case, 'fav_count' column) using pre_get_posts filter? If not, is there any other way to do this before the main query is run, without modifying WP core files?

  • Is there a reason you have a custom column instead of using post meta data? – Chris Morris Feb 2 '15 at 12:05
  • Yes - optimization, avoiding JOINs. It's a pretty big website with lots of visitors and over 300.000 posts. – NoviQ Feb 2 '15 at 12:14
0

I found out the answer myself.

There are other filters that let you adjust the query before it's run: http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses

The posts_clauses filter runs before the query gets executed and is essentially the sum of all filters that run immediately before it.

Apart from posts_clauses, there are several more precise filters that modify only a single specific clause:

  • posts_where_paged
  • posts_groupby
  • posts_join_paged
  • posts_orderby
  • posts_distinct
  • post_limits
  • posts_fields

So in my case, all I had to do was to use posts_orderby filter.

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.