I want to change the Wordpress main query on a fixed page.

Asked 2 years ago, Updated 2 years ago, 96 views

The following description has been added to function.php:

On a fixed page called season, I would like to rewrite the request parameter y to the main query that retrieves all the articles that hit the custom field year in the channel custom post.

Is it possible to rewrite it to the desired behavior in $query->set?
Is there no problem with the function to hook above?Please let me know.

add_action('init','my_init');
function my_init(){
    global$wp;
    $wp->add_query_var('y');
}
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts($query){
    if($query->is_page('season')&is_main_query()){

        $year=get_query_var('y');
        if($year){
          // I want to write here
        }
    }
}

php wordpress

2022-09-29 21:22

1 Answers

Rewrite the main query on a fixed page and replace it with a query for other posts is not a good behavior, so I made it possible with a subquery.

This post was posted as a community wiki based on @hata's comments.


2022-09-29 21:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.