WordPress.org

Ready to get started?Download WordPress

Forums

add_filter not working at all (4 posts)

  1. Brian Graham
    Member
    Posted 1 year ago #

    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 (http://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).

    http://pastebin.com/D0Mg5vMB

    Hopefully someone can tell me what's wrong.

  2. bcworkz
    Member
    Posted 1 year ago #

    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.

  3. Brian Graham
    Member
    Posted 1 year ago #

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

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

  4. bcworkz
    Member
    Posted 1 year ago #

    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'

Topic Closed

This topic has been closed to new replies.

About this Topic