Skip Navigation

[Resolved] Custom post with costom ctg: Display only post in subctgnot in parent ctg

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 6 replies, has 2 voices.

Last updated by Minesh 5 years ago.

Assisted by: Minesh.

Author
Posts
#1455069

I have:
- Category A
-- Subcategory A.1
---Sub- subcategory A.1.1
And i have custom posts in Sub-subcategory A.1.1
-- Sub-subcategory A.A.2
- Category B
I want to display Categories A & B and no post, then when I click Category A I want to display Subcategories A.1 and A.2 and no post, then when I clic Subcategory A1 I want to display Sub-subcategories A1.1 and then when I click Sub-subcategory A.1.1 I want to display custom posts.

I tri to do a view for subcategories and a view for post, and display conditional post only if category has no child categories but i don't know to achieve that with custom posts. In other project i use instead of custom posts WooCommerce products and in that case i used conditional [wpv-conditional if="( woo_has_product_subcategory() eq '0' )" evaluate="true"]

Please help my to achieve that with custom posts and custom categories.

#1456029

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

To display such category/subcategory/post list. You need to follow the following steps.

1)
Create a taxonomy view of your taxonomy (Category). Name this view as "top-category-view"
=> Uncheck the checkbox "Don't show empty terms" at the section "Query Options"
=> Add following code to its loop output section [You can add fields as you wanted to loop output]:

 <wpv-loop>
<a href="?taxonomy_id=[wpv-taxonomy-id]" target="_self" >
        <h3 class="image-link-text">[wpv-taxonomy-title]</h3>
</a>  
 </wpv-loop>

2)
Add following code to your current theme's functions.php file:

add_filter( 'wpv_filter_taxonomy_query', 'func_filter_category_subcategory_posts', 99, 4 );
function func_filter_category_subcategory_posts($tax_query_settings, $view_settings, $view_id) {
    if($view_id == 9999){
        (!isset($_GET['taxonomy_id']))? $tax_query_settings['child_of']=0:$tax_query_settings['child_of'] = $_GET['taxonomy_id'];
    }
    return $tax_query_settings;
}

Where:
- Replace 9999 with your taxonomy view ID

3)
Create a new post View to list ALL posts belongs to the lowermost taxonomy term, say "view-posts-belongs-to-term"
=> Add new query filter from the "Query Filter" section for taxonomy as given under and save it, For instance, my taxonomy is "Product Categories"

Select posts with taxonomy: Product Categories slug in one of those set by the URL parameter wpvproductcat
eg. <em><u>hidden link</u></em> 

=> add fields to display your post information as required at "loop output" section, for example [wpv-post-title] etc..etc.. and save the view

4)
Create a new page namely "Posts List" and add view "view-posts-belongs-to-term" created at step #3

5)
=> Add the following code to functions.php file or "Custom Code" section offered by Toolset.

add_filter( 'wpv_filter_taxonomy_post_query', 'func_list_post_or_taxonomy', 20, 4 );
function func_list_post_or_taxonomy( $items, $tax_query_settings, $view_settings, $view_id ) {
    if($view_id == 9999){
         
        foreach( $items as $item ) {
            $children = get_term_children( $item->term_id, 'product_cat');
             
            if(count($children)==0){
                $display_posts =$_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST']."/posts-list/";
                $item->name = '<a href="'.$display_posts .'?wpvproductcat='.$item->slug.'" target="_self" >'.$item->name.'</a>';
            }else{
                $item->name = '<a href="?taxonomy_id='.$item->term_id.'" target="_self" >'.$item->name.'</a>';
            }   
        }
    }
    return $items;
}

Where:
- Replace 9999 with your original taxonomy view ID
- Replace 'product_cat' with your original taxonomy slug

6)
Create a new page and add the "top-category-view" view to list categories and it should work as per your need when you click on the taxonomy term link. Please let me know if you need further help.

You can add the code I shared either to functions.php file of your theme or "Custom Code" section offered by Toolset.
=> https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/

#1461681
Screenshot 2020-01-16 at 11.48.07.jpg
Screenshot 2020-01-16 at 11.48.47.jpg
Screenshot 2020-01-16 at 11.49.10.jpg
Screenshot 2020-01-16 at 11.49.30.jpg

Hello Minesh,
I do everything but something not work. In my custom category list:
- some category has the link: hidden link
- some categories has the link: hidden link
If i click the first link is loading the page with the same content
If i click the second link i receive a 404 page.
Please tell me what i do wrong?
Page with categories list: hidden link
Page with products list: hidden link

#1461843

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please send me admin access details and tell me where exactly you added the code I shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#1461873

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks for the access details but I do not have enough permissions as I am not able to see the Toolset menu in the admin left bar.

Can you please give my user full permissions so that I can proceed further.

#1462047

Try again now please

#1462055

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks, now it's working.

So, I've added the shortcode as given under:

[wpv-view name="top-category-view" cached="off"]

And, I've changed the following lines of the code within "wpv_filter_taxonomy_post_query" hook code:

$protocol = ($_SERVER['HTTPS']=='on')?'<em><u>hidden link</u></em>':'<em><u>hidden link</u></em>';
$display_posts = $protocol.$_SERVER['HTTP_HOST']."/post-lists/";

Regarding:
- some category has the link: hidden link
==>
if the current category will have the child category (sub-categories available) then it will give you the link like above

- some categories has the link: hidden link
==>
If there is no child category available, it will show you the posts belongs to the current category.

Can you please check now and tell me it works: hidden link