0

I have some option in some of my wordpress tags and I'm trying to determine (in single.php) if one of those tags has options and if one of them (options) is equal to a specific value.

This is what I got so far but I'm a bit lost on how to move forward:

// get all post tags
$posttags = get_the_tags();
    
if ($posttags) {
    foreach($posttags as $tag) {
        // get the tags ID
        $tag_id = $tag->term_id;
        // putt all the ID's on an array
        $all_tags_id = array($tag_id . ', ');

... and I don't know how to go any further from here.

I guess I first need to isolate the ones that have options and then see if any of them has the option that I need empty OR not?

Help needed...

3
  • Can you be more specific what you mean by a tag having options? Are you talking about term meta? Options are stored in the options table and are site wide, they aren't specific to a tag or post
    – Tom J Nowell
    Commented Oct 10, 2020 at 12:58
  • @TomJNowell in my site, some of the tags have specific options. See this here to understand.
    – bpy
    Commented Oct 10, 2020 at 13:05
  • 1
    Oooh, they are not options, that's term meta, but if you're making the posts do different things based on the term meta then are you sure it was ever meant to be term meta? Next you'll need to query posts that have terms that have meta and it all falls apart because you can't do that.
    – Tom J Nowell
    Commented Oct 10, 2020 at 17:17

1 Answer 1

1

If you have a term, you can retrieve its meta via get_term_meta, this function works the same way as get_post_meta only it takes a term ID not a post ID

E.g.

if ( get_term_meta( $term_id, 'key', true ) === 'value' ) {
    // it has the value
}

Once you know this, it's just basic logic of looping over each term and checking them, which you already do in the code in your question

5
  • My problem is to find what term has meta and a specific value inside. Note that I have several tags on each post and only one tag may have some meta with specific value. Hope made it clear. Thanks!
    – bpy
    Commented Oct 10, 2020 at 22:09
  • You would have to loop through them and check each one, but you already have a loop so a basic if statement should do the trick. Which part are you having trouble with?
    – Tom J Nowell
    Commented Oct 10, 2020 at 22:43
  • I'm having trouble finding how to echo the ID of the tag that has "that value". Let me give this example: In a post I may have 4, 6, 8 or more tags. Only one OR two of those tags will have term meta. And from those, only one tag, will have term meta with "mine" value. In my form, I set it this way: ($tag_meta['this_is']== 'mine'). I need to discover what tag_ID has mine isset.
    – bpy
    Commented Oct 10, 2020 at 23:40
  • 1
    The answer to that is in my answer, you loop over each tag, and call get_term_meta to retrieve the value, and compare it. If it matches then you've found it, there's an example in the code block in my answer that does exactly what you're asking how to do
    – Tom J Nowell
    Commented Oct 11, 2020 at 1:29
  • Not quite the solution I needed. But it pointed me out in the right direction. Thanks!
    – bpy
    Commented Oct 11, 2020 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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