0

I made a new page in admin panel like this. I want my Query here to be main_query so the other plugins could hook it, modify limits, etc...

add_action('admin_menu', function (){
    add_submenu_page('edit.php?post_type=ticket', __('Board', 'askanban'), __('Board', 'askanban'), 'edit_posts', 'askanban', array( $this, 'askanban_display_board' ), 1);
});

public function askanban_display_board(){
        $args = array(
            'post_type'              => 'ticket',
            'posts_per_page'         => - 1,
        );
        $query =  new \WP_Query( $args );
        $query->is_main_query(); // FALSE HERE!!!!!!!
        $posts = $query->posts;
        print_r($posts);
}

I read tons of info like this How to know which one is the main query? , this https://wp-qa.com/can-i-set-my-wp_query-to-be-a-main-query or When should you use WP_Query vs query_posts() vs get_posts()? I tried to use get_posts() and even tried to set $GLOBALS['wp_query'] = new \WP_Query( $args ) or $GLOBALS['wp_the_query'] = new \WP_Query( $args ); Noting helps. The other plugin can't see this query as "main". For this "other plugin" there is no main query on this page. Why?

2
  • The main query is the WP core query running for the page. I doubt you can replace, there should be no need to replace it, and I'm certain that you shouldn't replace it.
    – vancoder
    Apr 14, 2022 at 21:11
  • But for now my page has no main query. Let's ask another way: "How to make main query for custom page in Wordpress?" What is "the best practice" for it? I don't want to make a workaround. This query should be a real main loop.
    – Movs
    Apr 15, 2022 at 7:24

1 Answer 1

0

And the anwser to my question: Don't make new WP_Query object. Don't try to overwrite main query. Use global $wp_query; In my example the function should be like this:

public function askanban_display_board(){
    global $wp_query;
    $wp_query->set('post_type', 'ticket');
    $wp_query->set('posts_per_page', -1);
    $posts = $wp_query->get_posts();
    print_r($posts);
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.