pre_get_posts Remove posts based on meta value with ‘post__not_in’

Try this version: // Get all posts with the access level of ‘Member’ function members_get_member_posts() { $post_ids = wp_cache_get( ‘wpse61487_members_posts’ ); if ( false === $post_ids ) { $post_ids = array(); $args=array( ‘post_type’ => ‘any’, ‘meta_key’ => ‘_members_access_role’, ‘meta_value’ => ‘member’, ‘post_status’ => array(‘publish’,’private’) ); $protected_posts = get_posts($args); if($protected_posts) { $post_ids = wp_list_pluck( $protected_posts, ‘ID’ … Read more

Can not switch the queried post in pre_get_posts hook

Huh. I double-checked. On my local server, this bit of code successfully changes the post: function test_redirect() { global $wp_query; $wp_query->set(‘page_id’, 5); } add_action(‘pre_get_posts’, ‘test_redirect’); You’re going to have to do a bit of debugging. On a dev server, var_dump the $wp_query variable a few key places to check that it has what you expect … Read more

If orderby parameter using pre_get_posts is the same for multiple posts what fallback does the query use?

For identical values they’ll be in whatever order they’re returned by the database engine, which would typically be the order they were inserted in the database. Anyway, the question you’ve linked is old, you can order by multiple columns with WP_Query: $query->set( ‘orderby’, array( ‘menu_order’ => ‘ASC’, ‘date’ => ‘ASC’ ) ); pre_get_posts acts on … Read more

How to use pre_get_posts?

Remove all query code from the template and just leave the default loop. In your example code you’re overwriting the query in the template, in the code you pasted in the comments, you’re running an entirely new query. These are both unnecessary when using pre_get_posts. Put your pre_get_posts code in functions.php. You don’t have to … Read more

Sort custom post types by last name in the backend

If you want to override the default- and the title ordering, and order instead by the last word in the title. /** * Override default- and title ordering in the backend, for the ‘speaker’ custom post type. * * @link https://wordpress.stackexchange.com/a/202154/26350 */ add_action( ‘pre_get_posts’, function( \WP_Query $q ) { if ( is_admin() && $q->is_main_query() && … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)