Problems to list the posts of a selected category without listing the posts of the children categories

Closed
REHub - Price Comparison, Affiliate Marketing, Multi Vendor Store, Community Theme REHub - Price Comparison, Affiliate Marketing, Multi Vendor Store, Community Theme September 28, 2018
Login to reply
Sergio Sergio
5 years ago

Thanks!!

Igor Sunz Support Agent
5 years ago

better to add this code again in core files. It's possible to overwrite it in grand child, but we put new functions in this code, so, better to keep it always updated and not overwritten 

Sergio Sergio
5 years ago

Yes yes and yes!! Many Thanks!!

Every time I update the theme I have to modify this code? Or is there any option to put it in the rh-grandchild-func.php?

Greetings.


Igor Sunz Support Agent
5 years ago

try next

if($filter_args->cat !='') {
$arrayforclean = $filter_args->cat;
$catidsclean = wp_parse_id_list($arrayforclean);
$args['category__in'] = $catidsclean;
}
Sergio Sergio
5 years ago

My purpose is to list the posts of the selected categories.

All Users 1 id=98

    Friends 1.1 id=102

        Friends 1.1.1 id=110

        Friends 1.1.2 id=111

    Friends 1.2 id=104

For example, list cat 102 and 104, but not 98, 110 and 111.
For example, list cat 98 and 102, but not the rest, not 104 (children of 98) and not 110,111 (children of 102)

With if ($filter_args->cat !='') {$args['cat'] = $filter_args->cat;} list the child categories, and despite putting the other categories excluded, it does not work, because it keeps listing the cat children.

If I use  {$args['category__in'] = $filter_args->cat;} Only list the first category that I write, if I put three categories to list, It only list the first...

Thanks.

Igor Sunz Support Agent
5 years ago

What do you want to have in final? If your purpose is to show parent (top)
category but exclude posts from child categories - you can use category as
usual, then add categories which you want to exclude. I tested this and
it's working https://www.screencast.com/t/7rdGEmehpXS

Sergio Sergio
5 years ago

If I use Custom post type and taxonomy with the taxonomy category slug:

1.- If I use one slug, it list this category and their children Posts... I don't want this, I don't want the children posts. And in this case it not run the code to not list the children posts:

if ($filter_args->cat !='') {$args['category__in'] = $filter_args->cat;}

Because this code, I think it only run for ids, not slugs.

2.- If I write for example two slugs, separated by     ,    it not list nothing. It seems that this functions/shortcode, the field term slug only support one slug...

Thanks.

Igor Sunz Support Agent
5 years ago

use custom taxonomy as I wrote. it must be "category". Everything must be
as on my screenshot. You can add slug of category in next field after
"category" field. SLUG is NOT EQUAL to ID. You can see category slug in
Posts - categories

Sergio Sergio
5 years ago

Hello, sorry but I don't undertand you.

Initial, I have this with this code: if ($filter_args->cat !='') {$args['cat'] = $filter_args->cat;}

[grid_loop_mod columns="3_col" aff_link="1" cat="102" cat_exclude="110,111"]

It list the post categories 102, 110 and 111.

Here the categories:

All Users 1 id=98

    Friends 1.1 id=102

        Friends 1.1.1 id=110

        Friends 1.1.2 id=111

    Friends 1.2 id=104

With this code: if ($filter_args->cat !='') {$args['cat'] = $filter_args->cat;}  --> List the parents and the childs categories.


I change the code with this, to list the id post categories without their children.

if ($filter_args->cat !='') {$args['category__in'] = $filter_args->cat;}

I try with this shortcode:

[grid_loop_mod columns="3_col" aff_link="1" cat="102,104"]

But it only list the post of the first id of the category, the 102.

If I write this:

[grid_loop_mod columns="3_col" aff_link="1" cat="104,102"]

It list the post of the category 104.

I don't know why not list the post that are in the id category 104 and the post that are in the id category 102. Maybe i only accept one id category this shortcode?

If I use Custom post type and taxonomy with the taxonomy category slug:
category102, category104 (like your image), it occurs the same, only list the posts of the first slug.


Also, I read here: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

That wiht $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) );

This shows posts that are in both categories 2 and 6. 

This is not for me.

Thanks.



Igor Sunz Support Agent
5 years ago

You can also try next settings https://www.screencast.com/t/vD8AfqTLAN9
this must choose only one category without child (this is for your previous
question)

Igor Sunz Support Agent
5 years ago

[grid_loop_mod columns="3_col" aff_link="1" cat="98,102"] will show posts
which belong to category 98 and 102 in the same time. If you want to have
98 OR 102, do changes which I described above

Sergio Sergio
5 years ago

Thanks, now, in cases that i try to list 2 categories, for example:

[grid_loop_mod columns="3_col" aff_link="1" cat="98,102"]

It only list the first id category (98), the second id does not list it (102). I want to list any post with the id category 98 and any post with the id category 102...

Greetings.


Igor Sunz Support Agent
5 years ago

No, because child categories are part of parent. You can do this only with
core customization

if ($filter_args->cat !='') {$args['cat'] = $filter_args->cat;}

to

if ($filter_args->cat !='') {$args['category__in'] = $filter_args->cat;}

in shortcodes/module_shortcodes.php

Sergio Sergio
5 years ago

Hello,

I have a problem with my posts lists. I do not know if I'm doing something wrong.

I use for example:

[grid_loop_mod columns="3_col" aff_link="1" cat="102" cat_exclude="110,111"]

Here the categories:

All Users 1    id=98
    Friends 2    id=102
        Friends 2.1    id=110
        Friends 2.2    id=111

Wiht this structure, I want to list the posts that are in the category Friends 2 id = 102, but the results of the posts contains the posts that are in the id 102, but also the children posts, the id 110 and the id 111.

I try to exclude the children posts, but it doesn't matter, they list all the posts include the children posts.

Is it possible to list the posts of a selected category, without also listing the posts of all their children?

Thanks.