WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add query variable to single page (6 posts)

  1. glennwelker
    Member
    Posted 5 years ago #

    Pretty basic request.

    I would like to add a query variable to further define a custom query on a single page.

    The only information I can find keeps pointing to the query object and is more appropriate for plugins. Is this possible from a theme?

    I have a page template for the page in question and I have a sidebar navigation that ideally would point to the same page with an additional query variable that I could use to modify my query.

    Thanks for the help.

  2. Jeremy Clark
    Moderator
    Posted 5 years ago #

    It might be easier if you explain what you want to do instead of how you want to do it, usually there is a much simpler way that you've might have overlooked.

  3. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    The only information I can find keeps pointing to the query object and is more appropriate for plugins. Is this possible from a theme?

    Yes. Put the code in the theme's functions.php file. This code is loaded in a way very similar to a plugin, and it's how themes can define custom code.

    I would like to add a query variable to further define a custom query on a single page.

    add_filter('query_vars', 'add_my_var');
    function add_my_var($public_query_vars) {
    	$public_query_vars[] = 'some_unique_identifier_for_your_var';
    	return $public_query_vars;
    }
  4. glennwelker
    Member
    Posted 5 years ago #

    OK.

    Essentially, I have a single page that lists books. Each book is a post in a single category. Furthermore, those posts have 1 or multiple tags.

    For example,
    Microsoft Office Outlook 2007
    tags, outlook, office, and 2007

    This page template by default lists all posts in this category. All is fine so far. What I would like to do, is use URL links in a navigation menu to filter those results.

    So, if they click on a link that says, Outlook 2007, I would like to change my query for this page to only display those posts that are have tags of 2007 and Outlook.

    If I use the example above, can I just add the query var to the permalink and then use the get_query_vars to parse out the details I need?

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Once the query var has been added like I show above, then you can append it to any URL (as a GET variable, like ?my_var=whatever) and receive its value back anywhere you like:

    $myvalue=get_query_var('my_var');
    echo $myvalue; // outputs "whatever"

    Note that of course you can bypass all this and just use $_GET and $_REQUEST and such, but with a query_var, you can also make use of the internal rewrite functions and thus integrate your var right into the URL, if you so choose.

  6. glennwelker
    Member
    Posted 5 years ago #

    Many thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic