Search query alteration not working for meta values
Search query alteration not working for meta values
Search query alteration not working for meta values
For those running into issues in the future, this is what I did: I made my pre_get_posts function that queries the taxonomy as normal for testing if an taxonomy ID is not in a list: $q->set( ‘tax_query’, array(array( ‘taxonomy’ => ‘tax’, ‘field’ => ‘id’, ‘terms’ => tax_get_inactive(), ‘operator’ => ‘NOT IN’ ))); Then, I implemented … Read more
$qry->is_main_query() will be true on every main query on your site, front end and admin. You need to add another more specific condition to target only the page you’re trying to modify the query on, like is_post_type_archive(‘trucks’).
Awesome. This works! Thanks @Milo /*Change results based on the current user’s role and status */ function posts_for_current_role($query) { global $pagenow; if( ‘edit.php’ != $pagenow || !$query->is_admin ) return $query; if( current_user_can(‘editor’)): //Add query where clause to status equals “working”. $query->set( ‘post_status’, array( ‘working’, ‘draft’ ) ); endif; if( current_user_can(‘administrator’)): $query->set( ‘post_status’, array( ‘published’) ); … Read more
As with any custom tables in your database, you can override the SQL that WP_Query ends up using. For this use case, I’d suggest not using WP_Query’s arguments and building your own function to do this instead as it could simplify the logic and complexity. <?php add_filter( ‘posts_clauses’, ‘my_custom_wp_query_posts_clauses’, 10, 2 ); /** * Custom … Read more
I have found the solution to my question and I share it. I hope someone else will find it useful in the future. I am not a developer and surely the code can be improved, but this one worked for me for what I wanted. I needed to show a list of posts ordered by … Read more
pre_get_posts filters run before WordPress has figured out the main query, which is used to pick the template. For this reason you can’t put it inside the template, it has to run before the template is picked, not afterwards. For this reason, it has to go inside your themes function.php or in a plugin
This issue shouldn’t happen if you’re using the classic editor, however, if you’re using the block/Gutenberg editor which uses the REST API, then that issue can be fixed by using the wp_after_insert_post hook instead. Excerpt from https://make.wordpress.org/core/2020/11/20/new-action-wp_after_insert_post-in-wordpress-5-6/: The new action wp_after_insert_post has been added to WordPress 5.6 to allow theme and plugin developers to run … Read more
Not the way you’re doing it, no. When the pre_get_post action is run any WP_Query queries run will be modified by the callback. Actions are not persistent, so when you run add_action() inside an AJAX callback that action is only going to be applied for that request, and you’re not running any queries in the … Read more
Update From your comments, it seemed that you’re having a hard time understanding about the WordPress main query and the is_main_query() method in the WP_Query class, so hopefully the following would help clear your doubts: Please check codex.wordpress.org/Query_Overview and learn how WordPress determines what posts or pages to display on a page. The main query … Read more