WAIT: Get_category() doesn't work on Categorie Archive-Pages

Bricks Version: 1.5.4 (compared to 1.4)

Hey there,

I’m building a product archive based on categories and all products are related to a vendor. I have two CPTs (products / vendors) and each product is related to a vendor using Metabox Relationship. This is not a WooCommerce project, it’s just about two simple CPTs related to each other.

What I have done is creating a Archive-Template which outputs all vendors and then queries after all products that belongs to each partner.
What I want to output is only those products that have the current archive-category assigned. So I have for example a category called “shirts”.

On the Archive https://example.com/category/shirts I would then output all vendors and in an inner query i would output all products that have the cat “shirts” and are related to that vendor (very simplified example)

I am modifying the query-vars using the corresponding filter (Filter: bricks/posts/query_vars – Bricks Academy).

In bricks 1.4 the following code to retrieve the current category slug worked perfectly fine:

$cat = get_category( get_query_var( 'cat' ) );
$slug = $cat->slug; // just for example 

In Bricks 1.5.4 the same code produces the following error:

Warning: Undefined property: WP_Error::$slug in /var/www/vhosts/faunt.de/01-sites/faunt/faunt-stage/wp-content/themes/bricks/includes/elements/code.php(159) : eval()'d code on line 7

What is going wrong here? Did Bricks change anything regarding the template architecture under the hood?

Thanks in Advance for your help!

Cheers

Hi Wolfgang,
Thanks so much for your report!

You should check if the variable is set:

$cat = get_category( get_query_var( 'cat' ) );
$slug = isset( $cat->slug ) ? $cat->slug : ''; // just for example

Does that work for you?

Best regards,
timmse

Hey @timmse

Thanks for your fast response. I think my topic was a bit misleading in communication :smiley:

I don’t really use that code, and i don’t output that code I’ve written in that post. I have a lot of validation inside my code, but while debugging I’ve nailed the problem down to what I’ve written, i can’t access the category information while i am on a cat archive.

So in short:
Just wanted to point out that I cannot get the category slug while I am on a category archive…which should work on every WP category template in a normal theme template .php file… But it doesn’t really work

Hope that is more clear, if not please let me know, maybe i can find another way to tell you what I exactly mean! :wink:

What I mean is that your code doesn’t solve the problem that I cannot access the category slug on a category template, which should work.

Of course the error disappears but shouldn’t I be able to access those information on that template, in 1.4. it worked.

In your URL (/category/shirts) there is no query (?abc=def) to get values from. Cause that’s what get_query_var() does, isn’t it?

I think you want to use get_queried_object()->slug.

I don’t think that get_query_var() is referring to a query-parameter in the URL. At least the Wordpress documentation points that:

Retrieves the value of a query variable in the WP_Query class.
See here: get_query_var() – Function | Developer.WordPress.org

In my understanding it should retreive the argument from the main-query, which has the category set on the archive template page?! And also it wouldn’t really explain why the same code works in 1.4 and doesn’t work anymore in 1.5.4

I’m not on my main PC now, where the Project is installed locally… But I think I’ve tried your solution (gonna double check that again tomorrow) as well as other several variations to retreive the category slug which didn’t work either. I’ve turned off all other plugins, which also didn’t have any effect on the result… :frowning:

What’s really weird is that stuff like https://developer.wordpress.org/reference/functions/get_the_archive_title/ don’t output the category name but the name of the CPT… If i use the dynamic tag from Bricks it outputs the category title as archive title… strange somehow in my opinion!

You’re right. The function is not just working with the URL. My bad.

BUT… I just noticed: For me both things work. The get_query_var() code you provided in your initial post as well as things like get_the_archive_title().

Hey @aslotta
Many thanks for testing it out!
That’s very interesting, I’m out today and gonna test it in the evening again… I cannot imagine of anything that causes that, specially because I have turned of every plugin to make sure that there are no conflicts.

At least I know now, that it’s not related to the 1.5.4 version, maybe somehow related to some settings in WP or so, gonna double check that. Thanks again! :wink: