最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Checking if one of the post tags has options

programmeradmin0浏览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...

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...

Share Improve this question edited Oct 11, 2020 at 1:30 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Oct 10, 2020 at 12:51 bpybpy 2995 silver badges20 bronze badges 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
Add a comment  | 

1 Answer 1

Reset to default 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

发布评论

评论列表(0)

  1. 暂无评论