• add_filter('query_vars', 'recent_queryvars' );
    function recent_queryvars( $qvars ) {
        $qvars[] = 'month';
        return $qvars;
    }

    I have been trying to add the query variable of month with no success. The above code is inside functions.php of my theme; and I try to access the variable using get_query_vars( 'month' ); inside ../theme_dir/includes/recent.php. It returns nothing.

    Also I am using post name as my urls (https://domain.com/sample-post/).

    Please don’t tell me how to just access the variable, the issue is with add_filter, it doesn’t seem to work on query_vars but I know add_filter works because I use it to add a shortcode right above it in my functions file (see below).

    https://pastebin.com/D0Mg5vMB

    Hopefully someone can tell me what’s wrong.

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

    (@bcworkz)

    You don’t say what sort of form or url is being submitted, so I’m totally guessing. If the form has no field named month with a value or the url has no parameter named month with a value, there will be no query var for month.

    What you are providing in the filter is a name to look for. If the name does not show up in the request, no query var will be created for it.

    Thread Starter Brian Graham

    (@briangraham91)

    Here is a regular request: https://domain.com/sample-post/
    Here is a request with pagination: https://domain.com/sample-post/3/
    Here is a request with month: https://domain.com/sample-post/?month=4
    Here is a request with both: https://domain.com/sample-post/3/?month=4

    The variable month is never collected from the query string, with my OP code inside functions.php

    Moderator bcworkz

    (@bcworkz)

    Thank you for the extra information. I agree everything seems to be in order. Though you admonished us not to tell you how to access query vars, I strongly suggest trying this:
    get_query_var( 'month' );
    No ‘s’ in ‘get_query_var’

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add_filter not working at all’ is closed to new replies.