Using get_terms function in MB Views (Twig). The result is not as expected

Support MB Views Using get_terms function in MB Views (Twig). The result is not as expectedResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #46338
    BoatKungBoatKung
    Participant

    I’m using MB Views to create a shortcode that lists my custom taxonomy. In my Twig template, I use the get_the_terms function like this:

    
    {% set cats = mb.get_terms({ 'taxonomy': 'wd_portfolio_cat', 'hide_empty': false }) %}
    {{ mb.var_dump(cats) }}
    

    However, the output I get looks like this:

    
    array(2) {
      [0]=>
      object(MBViews\Renderer\Term)#3005 (3) {
        ["meta_box_renderer":protected]=>
        object(MBViews\Renderer\MetaBox)#2037 (0) {
        }
        ["data":protected]=>
        NULL
        ["term":"MBViews\Renderer\Term":private]=>
        object(WP_Term)#3006 (10) {
          ["term_id"]=>
          int(3)
          ["name"]=>
          string(24) "ทาวน์โฮม"
          ["slug"]=>
          string(8) "townhome"
          ["term_group"]=>
          int(0)
          ["term_taxonomy_id"]=>
          int(3)
          ["taxonomy"]=>
          string(16) "wd_portfolio_cat"
          ["description"]=>
          string(0) ""
          ["parent"]=>
          int(0)
          ["count"]=>
          int(2)
          ["filter"]=>
          string(3) "raw"
        }
      }
      [1]=>
      object(MBViews\Renderer\Term)#3007 (3) {
        ["meta_box_renderer":protected]=>
        object(MBViews\Renderer\MetaBox)#2037 (0) {
        }
        ["data":protected]=>
        NULL
        ["term":"MBViews\Renderer\Term":private]=>
        object(WP_Term)#3008 (10) {
          ["term_id"]=>
          int(4)
          ["name"]=>
          string(54) "บ้านแฝด / บ้านเดี่ยว"
          ["slug"]=>
          string(26) "single-semi-detached-house"
          ["term_group"]=>
          int(0)
          ["term_taxonomy_id"]=>
          int(4)
          ["taxonomy"]=>
          string(16) "wd_portfolio_cat"
          ["description"]=>
          string(0) ""
          ["parent"]=>
          int(0)
          ["count"]=>
          int(2)
          ["filter"]=>
          string(3) "raw"
        }
      }
    }
    

    When I run the same query with get_terms in PHP, the result is:

    
    array(2) {
      [0]=>
      object(WP_Term)#3069 (10) {
        ["term_id"]=>
        int(3)
        ["name"]=>
        string(24) "ทาวน์โฮม"
        ["slug"]=>
        string(8) "townhome"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(3)
        ["taxonomy"]=>
        string(16) "wd_portfolio_cat"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(0)
        ["count"]=>
        int(2)
        ["filter"]=>
        string(3) "raw"
      }
      [1]=>
      object(WP_Term)#3228 (10) {
        ["term_id"]=>
        int(4)
        ["name"]=>
        string(54) "บ้านแฝด / บ้านเดี่ยว"
        ["slug"]=>
        string(26) "single-semi-detached-house"
        ["term_group"]=>
        int(0)
        ["term_taxonomy_id"]=>
        int(4)
        ["taxonomy"]=>
        string(16) "wd_portfolio_cat"
        ["description"]=>
        string(0) ""
        ["parent"]=>
        int(0)
        ["count"]=>
        int(2)
        ["filter"]=>
        string(3) "raw"
      }
    }
    

    I’m not sure if this is a bug or something I’ve missed, but I expected both outputs to be the same because I cannot get data in protected property. Could you advise on this?

    Thank You.

    #46354
    PeterPeter
    Moderator

    Hello,

    The object in MB Views is a bit different from the WordPress PHP object. However, they work the same. You can use the loop to iterate through the cats and output each cat term. Here is an example:

    {% for cat in cats %}
        {{ cat.name }}
        <br>
    {% endfor %}

    Following the documentation https://docs.metabox.io/extensions/mb-views/#twig

    #46356
    BoatKungBoatKung
    Participant

    I previously attempted this, but I decided to give it another try. Surprisingly, it’s now working!
    I’m uncertain as to why—I must have overlooked something earlier.

    In any case, I appreciate your assistance

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.