• Resolved findshorty

    (@findshorty)


    I have a number of custom query args that my plugin uses, only a couple of which are actually related to changing the posts query. The rest of them are related to the plugin and perform such functions as outputting certain confirmation or error messages for example.

    My problem is this: how can I force WP to ignore the query args that are “mine”?

    As an example, my home page is a static page that contains a number of shortcodes to output the custom login form, a few links to some information and a link to the registration form. If a user logs in and that login fails for whatever reason, I go back to the home page appending ?err=LOGIN to the url (but it could actually be any of the custom query args I’ve specified). The home page now displays the Hello World post!?

    It seems that ANY query arg (that has previously been specified) appended to the URL will cause WP to ignore the static page and instead show the list of posts (of which there are none, this being a paged site).

    The only way I can think of making sure the page that holds the home page shortcodes is always shown when is_home() or is_front_page() is true, is hook into pre_get_posts, but this seems a bit janky.

    Is there a mechanism where we can register query args to be available to get_query_var() but not automatically kick off the posts query?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I think what’s happening is the query args are are appended to a default “blank” query which in part is 'post_type' => 'post'. You may be able to always add &post_type=page to URLs but it seems using ‘pre_get_posts’ seems cleaner to me. This is not janky at all, it’s really the best way we have to alter queries to our needs behind the scenes.

    It may take more than changing the post type to page to solve your issue, but ‘pre_get_posts’ is the solution. Whatever may be wrong, you can check the query vars in this action and adjust as needed to get the query that is correct for the situation.

    I’m rather surprised WP is trying to use your query args that have nothing to do with getting posts. It should ignore tags it does not recognize. Did you register these tags? If they have nothing to do with getting posts, there’s no reason to register them.

    Thread Starter findshorty

    (@findshorty)

    Yes I did register the args. I just assumed it was cleaner than having to access (and filter) the $_GET collection.

    I certainly didn’t think my args were going to affect the posts query, and funnily enough they are completely ignored by the main query on every page but the home page, which is a bit weird.

    I managed to find a solution late (very late!) last night.

    add_action('pre_get_posts', 'force_home' );
    function force_home( $query ) {
    	if(is_home() || is_front_page()) {
    		global $extravars;
    		foreach($extravars as $x)
    			unset($query->query[$x]);
    	}
    }

    Basically it emptied the query of any of my args – $query->set($x,NULL) didn’t work btw.

    The really strange thing was that my custom args were still available for use by the shortcodes in the home page. So get_query_var() still worked as expected even though I’d removed them from the main query.

    Still feels hackish to me to be honest and I’d love to know if there is a proper mechanism for registering args to be ignored by the main query, but for now it will do just fine.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Force wordpress to ignore my custom query args’ is closed to new replies.