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

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.

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Related documentation: Adding Custom Taxonomies to WordPress and Creating Archives

This topic contains 6 replies, has 2 voices, and was last updated by  williamm-5 1 week, 6 days ago. The support staff assigned to this topic is Luo Yang.

Viewing 7 posts - 1 through 7 (of 7 total)
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

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

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

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
http://wp-types.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://wp-types.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.
http://wp-types.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

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

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

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

Luo Yang
Forum moderator

Supporter languages: English (English ) Chinese (Simplified) (简体中文 )

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

The custom codes is out the range of Toolset support:
https://wp-types.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!

Viewing 7 posts - 1 through 7 (of 7 total)

You are not logged in. You can view support threads, but not post.
If you are already logged in, please refresh your browser.

Known issues and their solutions

We document known issues and their solutions in Toolset erratas. Save time by checking there first and seeing if what you are facing is a known issue. This list is kept short. We do our best to resolve all known issues with every release of Toolset plugins.

Support Policy

Toolset staff will help you use the Toolset plugins correctly, provide any information you need, help troubleshoot problems and advice on best-practices.

Please make sure to issue only one topic per each support ticket.

For more information, read our full Customer Support Policy.

Need custom programming work for your site? Check out our certified partners.