My client wants to show conditional info in the sidebar of blog posts for various categories. I want them to be able to edit the info for each category instead of it being hardcoded in.
My thought was to add custom fields to the category term page and they could edit it there.
But after a little research, looks like I need to pull this into the custom_field_value: get_post_meta(get_the_ID(), ‘_yoast_wpseo_primary_category’, true);
How can I get the current post’s ID from inside a taxonomy loop?
And that is outputting the category ID of the primary category. But it is still showing all of the sidebar_cta_title for all of the categories assigned to the post…? Do custom_field=" " and custom_field_value=" " not work with taxonomy term loops?
I just tested this out myself and it seems like you’re right that custom_field and custom_field_value don’t in fact exist as query parameters associated with a taxonomy term loop. I’m not familiar enough with how queries work in the backend of WordPress but it’s entirely possible that the mechanism that’s used by that query parameter when querying posts just doesn’t exist when querying taxonomy terms. If it were, I would assume it would have been included, since most of those query parameters are just piggybacking off existing PHP functions and weren’t all custom-built.
If you check out the Loop tag documentation you’ll see that there are also general-purpose attributes that you can use to filter your loop. Theoretically, they might be marginally less efficient than filtering your loop using query parameters, but they’re a lot more flexible. In your case, you could use field and field_value:
Remember that when you write a shortcode in L&L, you need to wrap it in a Shortcode tag so that it gets processed instead of just being interpreted as plain text. I haven’t worked with that feature of Yoast so I can’t tell you if using that shortcode is the right approach, but I can answer this question:
You’ve got the right idea that the current post’s ID doesn’t exist within a taxonomy term loop. You can always use variables to pass data from one loop context to another.
<Set current_id><Field id /></Set>
<Loop type=category post=current>
<Field title />
<Get current_id />
</Loop>
On the other hand, if you’re trying to put the current post’s ID into the value of a query parameter, you can just do that with <Field id /> because until you actually open the loop, the context of the tag is still in the current post, not in the post loop. In other words, while you’re defining your loop, you haven’t actually started the loop and therefore you haven’t changed the loop context. So something like this should work to pass the current post’s ID to the value of the field_value query parameter:
So I am showing the primary category has an ID of 118. And we are looping through all tax terms assigned to this post - outputs ‘CX Hub Test’ and ‘Security Test’. And we can see ‘Security Test’ matches that ID (so that is the primary category) so that’s what we would expect it to output here:
So I don’t know why it isn’t working, but I found a workaround. Since my shortcode is already figuring out the Primary Term ID for the current post, I can skip querying the ‘field_value’ all together and just pull that single term since we know the ID:
Oh, I think I see what’s going on. That _yoast_wpseo_primary_category field exists on your post, not the taxonomy term, right? And you’re trying to filter your loop down to only loop through the taxonomy term that has that number as its ID. If that’s the case, then I assume what you would actually want to write would be this:
The reason your earlier attempts weren’t working was because you were trying to loop through taxonomy terms that had a _yoast_wpseo_primary_category field that matched some value. But that field doesn’t even exist on taxonomy terms in the first place.
So that explains why that template didn’t work, but I think your “workaround” is probably just the right way to go about this in the first place. As far as I can see, your _yoast_wpseo_primary_category field and your [primary_category_id] shortcode are two ways of outputting the same data, right? If that’s the case, instead of running a shortcode at all, you could probably simply use the field and avoid the extra bit of processing and complexity that’s added when you use the shortcode.