Reading elements of a looper

Hello, I have a looper provider for the “Current Post Terms”. Inside the looper consumer I have put a text element that contains {{dc:looper:debug_consumer}}, so that I can see which data is available. Here is an example of this debug-output of one of the returned items:

Index: 1
Current Data:
object(WP_Term)#9084 (10) {
  ["term_id"]=>
  int(79)
  ["name"]=>
  string(8) "OptoDrum"
  ["slug"]=>
  string(8) "optodrum"
  ["term_group"]=>
  int(0)
  ["term_taxonomy_id"]=
  int(79)
  ["taxonomy"]=>
  string(14) "classification"
  ["description"]=>
  string(0) ""
  ["parent"]=>
  int(78)
  ["count"]=&>
  int(6)
  ["filter"]=>
  string(3) "raw"
}

In my specific example I want to access the “parent” field, but I cannot get it to work.

The following constructs work:
{{dc:looper:index}} {{dc:looper:count}} {{dc:term:name}} {{dc:term:description}} {{dc:term:slug}}

These constructs do NOT work:
{{dc:looper:field key=“parent” fallback=“xxx”}} (or any other key - always returns xxx)
{{dc:term:meta key=“parent”}} (or any other key - always returns noting
{{dc:query:query_var key=“parent”}}

How can I access the “parent” field?

Hi @striata,

Thanks for reaching out.
It will be very difficult to recognize the issue without investigating through the admin dashboard. Can you please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Thanks

See secure note above for log-in information.

As I said, I want to access the “parent” of each term and work with that (e.g. use it in the Conditions to show/not show a term). Maybe it is worth to add what it is that I want to achieve, because there might be an easier way?

Each of my posts can have several properties (example: size, color, …). I could make separate taxonomies for each of those. What I have now, instead, is a combined taxonomy, which I call “classification”. It is hierarchical. In the above example, the 1st-level entries would be"size", “color”, etc, and the children are the properties themselves.

In my case, each post can have zero to many properties belonging each parent. I want to show all the those child-terms that exist, filtered by parent (e.g. only the children of “size”).

I am looking for a way to do this with such a combined taxonomy.

Hi @striata,

Unfortunately, the given credentials are not working. Can you please check and send it once again.

Thanks

See secure note

Hi @striata,

Unfortunately, that is not possible in a usual way, you need to call the customize hooks using the Custom type of Looper Provider.

test-new-Content-Pro (75)

You need to add the following code into your child theme’s functions.php and call it in the Looper Provider as shown in the above screenshot.

add_filter('cs_looper_custom_subcat', function($result, $args) {
  $category = get_queried_object();
  $term_id= $category->term_id;
  $trm=get_term( $term_id, 'category' );
  return ['parent_cat'=>$trm->parent];
}, 10, 2);

And add the {{dc:looper:field key=“parent_cat”}} code to get the returned parent category.

Hope it helps.
Thanks

Dear @tristup, thanks a lot. I am not quite sure where to plug in that looper provider in my workflow / hierarchy of elements and loopers on the page.
But looking at this code, I am thinking that there should be a similar cs_custom_looper code that directly performs my end goal. Could you try and help me out with that as well?
This custom code should work very similar to the built-in “Current Post Terms”. The built-in function accepts one argument (“Taxonomy”), and it returns all terms of the current post that belong to “Taxonomy”.
I would like a function that accepts two arguments: In addition to the “Taxonomy”, it should also accept a term (either name or ID). If the given term exists within the taxonomy, and if it has children (or rather: if it has children that are assigned to the current post), then those children-terms should be return. Otherwise, the looper provider will return empty.
One can also think of it this way: Take all the assigned terms of the current post of the given taxonomy (just like “Current Post Terms” does), but return only those terms that have the given term (second argument) as parent.
Thank you!

Hi @striata,

You can use the custom hook within your existing Looper Provider and pass the {{dc:term:id}} as a parameter to the custom hook function as shown in the screenshot. You need to change the function a little, you can find that change in the below code.

add_filter('cs_looper_custom_subcat', function($result, $args) {
    $term_id= $args['cat'];
    $trm=get_term( $term_id, 'category' );
    return ['parent_cat'=>$trm->parent];
}, 10, 2);

And then, you need to add the {{dc:looper:field key=“parent_cat”}} code to get the returned parent category.

Hope it helps.
Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.