How to short circuit the below query via hooks such that the query is not run based on a condition? I tried using pre_get_posts filter but that is for posts queries only. The query runs as a part of class WP_Posts_List_Table __construct(). I understand that I can change the core wordpress code to short-circuit this but I am looking to do this via hooks without having to edit core wordpress files. For example, if is_admin(), the query should return a constant result instead of getting executed.

$this->user_posts_count = (int) $wpdb->get_var(
            "SELECT COUNT( 1 )
            FROM $wpdb->posts
            WHERE post_type = %s
            AND post_status NOT IN ( '" . implode( "','", $exclude_states ) . "' )
            AND post_author = %d",
  • can you provide more context for what the page it's running on is or where that code is located? A generic answer for all queries is very unlikely to be of use to you or solve your problem, if you can provide information about where/when/what the query is rather than a code snippet without context then a proper answer can be written. Did you write this code? Is it part of a plugin? WP Core?
    – Tom J Nowell
    Dec 3, 2023 at 20:01
  • Query runs as a part of WP_Posts_List_Table __construct(). I have added the same in the question.
    – dc09
    Dec 4, 2023 at 4:06
  • there's still a lot of information missing, but that gives a clue as to what you're doing, are you trying to override the custom post type list screen in the admin with data from your own tables? It's tempting to provide only technical details and constrain them as much as possible but it's hurting your question. Knowing what you're trying to do that lead to this at a high level/user level rather than an engineer level provides an enormous amount of useful debugging information that you've not shared. Right now it's not possible to answer this with the information given
    – Tom J Nowell
    Dec 4, 2023 at 9:03
  • Try to focus on the original problem and offer your solution as a suggestion, rather than offering your solution then asking how to implement it. If you're asking this to improve performance on screens or to replace author functionality then there are better ways to do this
    – Tom J Nowell
    Dec 4, 2023 at 9:05


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.