首页 > 解决方案 > WordPress - 传递 args URL 时出现 404 错误

问题描述

我制作了一个可以正常工作的自定义帖子“项目”,但我需要使用 URL 中的参数过滤项目。

喜欢:mywebsite.com/projects?type_project=wealth

但我每次都会收到 404 错误。

这是 functions.php 中的代码:

function my_pre_get_posts( $query ) {
    
    // do not modify queries in the admin
    if( is_admin() ) {
        
        return $query;
        
    }
    
    
    // only modify queries for 'event' post type
    if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'projets' ) {
        
        // allow the url to alter the query
        if( isset($_GET['type_projet']) ) {

        $query->set('meta_key', 'type_projet');
      $query->set('meta_value', $_GET['type_projet']);
      echo($_GET['type_projet']);
            
    } 
        
    }
  // return
  

    return $query;

}


add_action('pre_get_posts', 'my_pre_get_posts');

如果我尝试在archive-projects.php 中过滤,它工作正常。 我也尝试过刷新永久链接

如果有人可以帮助我,请(:

标签: wordpresshttp-status-code-404custom-post-type

解决方案


推荐阅读