D
D
Denis99992020-08-13 20:58:37
WordPress
Denis9999, 2020-08-13 20:58:37

After editing wp_query in pre_get_posts gives 404 page?

I simplified the code as much as possible, and after that, instead of the filtering page, it gives out the 404th:

add_action( 'pre_get_posts', 'action_function_name_11' );
function action_function_name_11( $query ) {
  
  if ( isset($_GET['research_me']) && $query->is_main_query() ) {

  $meta = array(
            array(
            'key' => 'test123',
            'value' => 'text5',
            'compare' => '='
            )
        );
        $query->set('meta_query',$meta );
  
  }
}


Everything works if you comment out the line: That is, if you do not edit the request according to metadata correctly, it will display a 404 page. It's like comparing warm to soft. Do you have any ideas why this is happening?
$query->set('meta_query',$meta );

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis9999, 2020-08-18
@Denis9999

pre_get_posts did not affect the query in any way, because such a topic, search results and wp_query construction took place in a shortcode

P
Pavel Chesnokov, 2020-08-17
@cesnokov

You are overwriting the parameters, but they must be added to the existing ones:

add_action( 'pre_get_posts', 'action_function_name_11' );
function action_function_name_11( $query ) {
   if ( isset($_GET['research_me']) && $query->is_main_query() ) {
      $meta = $query->get('meta_query');
      $meta[] = array(
         'key' => 'test123',
         'value' => 'text5',
         'compare' => '='
      );
      $query->set('meta_query', $meta);
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question