Support » Developing with WordPress » GET request results in 404

  • Resolved bumblebeegames


    Hi, guys.
    I am having a serious problem here and sincerely hope you can help me. I am trying to use GET requests to handle some WP query filtering of ACF posts (custom post type ‘services’).
    So I set up the following in my functions.php:

    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    function my_pre_get_posts( $query ) { 
        if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'services' ) {
            if( isset($_GET['services']) ) {       
                $query->set('meta_key', 'services-field');
                $query->set('meta_value', $_GET['services']);            
        return $query;
    add_action('pre_get_posts', 'my_pre_get_posts');

    However, running a <url>/services/?services=Something ends up showing my 404 page.
    Any idea what I might be doing wrong?
    Using WP4.9.5 with X Theme 6.0.4 and ACF Pro.
    Thanks a lot!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Addendum: Tests have shown that the function including ‘ if( isset($_GET[‘services’]) ) {‘ is being correctly triggered and it’s reaching the $query->set.

    When you register the services post type, the services query parameter has been reserved for use for finding posts of that type by slug. This is based on the query_var argument of register_post_type(). From the documentation:

    Sets the query_var key for this post type. Defaults to $post_type key. If false, a post type cannot be loaded at ?{query_var}={post_slug}. If specified as a string, the query ?{query_var_string}={post_slug} will be valid.

    So when you go to /services/?services=Something it’s trying to load a single services post with the slug Something.

    Your options for solving this are to either set query_var to false, preventing this from happening, or choosing a different parameter name for your filtering, other than $_GET['services'].

    Aaaah, alright, thanks so much! That fixed it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘GET request results in 404’ is closed to new replies.