Sort archive view with pre_get_posts hook

I solved this with a different approach. I used posts_join and posts_orderby filters. Do not forget to cast meta value as unsiged to avoid string ordering instead of int. //edit query join function hsa_story_join($join){ global $wpdb; $option = get_option(‘hsa_story_cats’); if(!is_admin() && is_category($option)) { $current_category = get_queried_object(); $join .= ” LEFT JOIN (SELECT post_id, meta_key, meta_value … Read more

Custom post type by ID in root folder

Try creating a custom rewrite rule and a custom rewrite tag. This should allow you to set up a regular expression to capture the ID and manipulate the query. Rewrite API/add rewrite rule « WordPress Codex https://goo.gl/kdEJd8 Rewrite API/add rewrite tag « WordPress Codex https://goo.gl/PVLzLh

Pre get posts for single post

First of all, $query object is passed by reference, you don’t need to return $query in pre_get_posts. Second, is_post_type_archive( ‘events’ ) will work just fine, you don’t need to use query->query_vars[]. Corrected code will look like this: function my_pre_get_posts( $query ) { if( ! is_admin() && is_main_query() && is_post_type_archive( ‘events’ ) ) { $query->set(‘orderby’, ‘meta_value_num’); … Read more

How would I use pre_get_posts to query another site for posts in my multisite network?

OK so for reference I ended up actually using switch_to_blog() and restore_current_blog(). I found no references whatever for querying multiple tables (except writing my own JOIN-statements) when followed the source code for the action pre_get_posts. A lot of advice was given to skip restore_current_blog() to save CPU-cycles but when I checked the source code for … Read more