PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] Return the ID of the selected terms to array

 March 12, 2022     arrays, function, php, wordpress     No comments   

Issue

My terms have an additional ACF true/false field. I need to get a list of all the term IDs with the field enabled and return a list of these IDs.

So far I have achieved such a code, but it returns me only one ID:

function terms_exclude_id_func()
    {
        $terms_control = get_terms([
            'taxonomy'      => 'product_cat',
            'hide_empty'    => false,
            'meta_query'    => [
                [
                    'key'       => 'glob_menu',
                    'value'     => '1',
                ]
            ]
        ]);
        foreach ($terms_control as $term_control) {
            $IDs = $term_control->term_id;
        }
        return $IDs;
    }

    echo terms_exclude_id_func();

I need echo terms_exclude_id_func(); to return something like this 688, 534, 827

I will be grateful for any help in this matter. I'm just learning myself)


Solution

in your foreach loop, $IDs must be an array, and you need to push the values into it, not redefine its value:

function terms_exclude_id_func()
    {
        $terms_control = get_terms([
            'taxonomy'      => 'product_cat',
            'hide_empty'    => false,
            'meta_query'    => [
                [
                    'key'       => 'glob_menu',
                    'value'     => '1',
                ]
            ]
        ]);
        $IDs = array(); // define the variable as an array
        foreach ($terms_control as $term_control) {
            $IDs[] = $term_control->term_id; // push to the array
        }
        return $IDs;
    }



Answered By - mindthefrequency
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing