Sort posts in dashboard using custom field; also include posts where field isn’t set
Sort posts in dashboard using custom field; also include posts where field isn’t set
Sort posts in dashboard using custom field; also include posts where field isn’t set
I was having the exact same problem on an archive page of a custom post type with a needless extra query; for pagination reasons. Searching for the same solution I found this post on Stack Overflow https://stackoverflow.com/questions/21303743/new-wp-query-or-pre-get-posts-to-view-all-posts-for-custom-post-type I had a play and added this line of code into the function. if( ! $query->is_post_type_archive()) return; So … Read more
I’m not sure why, but it had to do with the title in my ordering, once I removed that everything started working fine. I’ll leave this open for anyone who knows why. $query->set(‘orderby’, ‘meta_value_num title’); – Does not Work $query->set(‘orderby’, ‘meta_value_num’); – Works!
Adding custom post types to the default loop, yet only posts that has terms from the core taxonomies
The pre_get_posts runs before WP_Query has been setup. Some template tags and conditional functions that rely on WP_Query will not work. you will need to work directly with the query vars, which are passed to the pre_get_posts hook as an argument. See the more information on the following pages: pre_get_posts and using pre_get_posts Try using … Read more
This is untested, but I think it should work. Add the filter in pre_get_posts if it’s your post type archive, otherwise remove it for any queries that follow the post type archive main query. function wpd_add_posts_orderby( $query ){ if( $query->is_post_type_archive(‘your-cpt’) ){ add_filter(‘posts_orderby’, ‘edit_posts_orderby’); } else { remove_filter(‘posts_orderby’, ‘edit_posts_orderby’); } } add_action( ‘pre_get_posts’, ‘wpd_add_posts_orderby’ );
function exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( ‘cat’, ‘-5377’ ); $query->set( ‘orderby’, ‘date’ ); $query->set( ‘order’, ‘DESC’ ); } } add_action( ‘pre_get_posts’, ‘exclude_category’ ); Use ASC or DESC for order. Add code to child themes functions file. Tested and works.
You need to check the information that their is in action variable. $action = (isset($_REQUEST[‘ac’])) ? $_REQUEST[‘ac’] : ‘none’; var_dump(“action: “+$action); With this line, you will be able to review it’s content and you will see that you don’t get the correct information. I guess that your dev database is out off synch with your … Read more
I doubt that ever worked by itself. WP_Query operates on posts database table. On the bottom SQL level to make use of post metadata you need to not only say what metadata you are looking for, but also instruct MySQL how metadata table connects to your posts table (JOIN realm). I would attempt to keep … Read more
Filter WP_Query output before it is accessed (pre_get_posts)?