Skip Navigation

[Resolved] Conditional display parent taxonomy term based on child post's count?

The Toolset Community Forum is closed, for technical support questions, please head on to our Toolset Professional Support (for paid clients), with any pre-sale or admin question please contact us here.
This support ticket is created 7 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the community support forum for Types plugin, which is part of Toolset. Toolset is a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients and people who registered for Types community support can post in it.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 6 replies, has 2 voices.

Last updated by williamm-5 7 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#558086

my situation as follows:

I had setup 2 relational CPT, let's say:
1. parent is "market", and parent have taxonomy "market category".
2. child is "product".

I set up two view.
1. one is "market category" taxonomy view of parent "market"
2. another view is "product" view, nested by parent's "market", filter by parent taxonomy view

what i am trying to do.

now, i want to use a shortcode in the view loop to conditional display the taxonomy term.

function get_taxonomy_term_childpost_infos( $atts ) {
    extract( shortcode_atts( array(
        'parent_id' => '',    // Set the parent post type ID
        'parent_type' => '', // Set the parent post type
        'child_type' => '', // Set the child post type
    ), $atts ) );

    if($parent_id != '')
    {
      $childargs = array(
        'post_type' => $child_type,
        'numberposts' => -1,
        'meta_key' => '_wpcf_belongs_'.$parent_type.'_id',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_query' => array(array('key' => '_wpcf_belongs_'.$parent_type.'_id', 'value' => $parent_id))
      );
    
      $totalChildCount = count(get_posts($childargs));
       return $totalChildCount;
    }
}

add_shortcode( 'get_taxonomy_term_childpost_info', 'get_taxonomy_term_childpost_infos' );

I want to use: [get_taxonomy_term_childpost_info parent_id="parent_id" parent_post="market" child_post="product"] in "market category" taxonomy view to Conditional display parent "market" taxonomy term based on child post's count (which is belong to specific terms).

question:
1. is it OK for that ?
2. if it's ok, how to pass parameter from "market category" taxonomy view to my shotcode's three parameter: "parent_id", "market" and "product" ?

#558098

Dear williamm,

I don't think it is OK, those views:
1. one is "market category" taxonomy view of parent "market"
2. another view is "product" view, nested by parent's "market", filter by parent taxonomy view
There isn't any direct relationship between them, in your case, it needs the third view, for example:
1) Top level view: taxonomy view
Query terms of taxonomy "market category"
display below view

2) 2nd level view: parent "market" post view
Query posts of parent post type "market", filter by Taxonomy filter:
Select posts with taxonomy: "market category" set by the parent Taxonomy View
https://toolset.com/documentation/user-guides/filtering-views-by-taxonomy/

Here you can use your custom shortcode, if you are going to use it in the Views shortcode [wpv-conditional] shortcode, please follow our document to setup it:
Using shortcodes in conditions
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/using-shortcodes-in-conditions/
and display the below view

3) 3rd level view: "product" view, filter by a Post relationship filter:
Select posts that are a children of the current post in the loop.
https://toolset.com/documentation/user-guides/querying-and-displaying-child-posts/?

#558135

Dear Luo,

Thanks, and it's not working.

2nd level is OK for connect parent and child relationship.

1) Top level view: taxonomy view for parent
Query terms of taxonomy "market category"
display below view

2) 2nd level view: parent "market" post view
Query posts of parent post type "market", filter by Taxonomy filter:
Select posts with taxonomy: "market category" set by the parent Taxonomy View

I can use shortcode at 2nd level and get child post count info.

but I can not put the shortcode in the the top level (to achieve total child post's account under tax terms ) which can be conditional display taxonomy terms.

I think it need to be add a terms field to store this information.

Luo, Any comments ?

#558145

It is expected result, that your custom shortcode won't be able to work in the top level view, since the top level view is for listing terms of custom taxonomy, it can not output the parent "market" post ID, in you case, you can setup another custom shortcode:
1) Accept the term's ID as parameter
2) In the custom shortcode function:
query parent "market" posts depends on the term's ID, then you will be able to get the parent "market" post IDs, and use them to get the child post counts.

#558168

Dear Luo,

You are right and i need to modify shortcode to query all the post ID's child post number under a terms.

Is there any reference, link or instruction on this. I still need your assistance.

following link is ref. I still need your help to write it in types's style way.

https://wordpress.stackexchange.com/questions/227468/get-posts-of-an-specific-term-of-a-custom-taxonomy

Thanks.

#558392

The custom codes is out the range of Toolset support:
https://toolset.com/toolset-support-policy/
I suggest you follow wordpress document to setup your custom PHP codes:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Taxonomy Parameters
Show posts associated with certain taxonomy.

#558456

Luo, Thanks very much!

The forum ‘Types Community Support’ is closed to new topics and replies.