I would like to pass a category name into the next page. I'm using Session. In functions.php I added:

function session_initialize() {

if ( ! session_id() ) {
add_action( 'init', 'session_initialize' );

In a page ( index.php ) I added:

$term = $wp_query->queried_object;
$cat_title = $term->name;
$_SESSION['varname'] = $cat_title;

In the next page ( single.php ) I added:

<?php if ( isset( $_SESSION['varname'] ) ) { echo $_SESSION['varname'];}; ?>

But it doesn't pass any value to the next page. If i change the code to this in index.php it works though:

$_SESSION['varname'] = "example";

Thanks for your help.


If it is working with a literal string ("example") but not with the variable, we can assume that the issue is with your variable.

You haven't provided all of the code for that section of index.php, but as it is I do not think you can expect that variable to hold anything.

First, you are using $wp_query. What is this variable? There is a global variable $wp_query, but you must first bring it into scope via global $wp_query. Are you doing that?

Second, the queried_object holds information about the queried object, which can sometimes be a post, sometimes a category, etc. These have different properties, so the way you've written your code is not particularly reliable.

If you are expecting a page to be that object, it should have a WP_Post object inside of it. That has a property called post_name instead of name.

A simpler way to get the post slug would be:

$slug = get_post_field( 'post_name' );

However, it looks like you might be trying to get a category, which does have a name property. But if the queried object is a page (like the front page it seems like you are using), it won't hold the category but instead the page.

If you're in the loop, I would instead use:

$categories = get_the_category(); //this gives you an array of term objects

You could decide if you wanted to pass just one of those categories (a post can have multiple categories) or all of them. Each item in the array would have the "name" property, which you could easily send.

In any case I hope that gets you closer to the solution you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.