• Resolved artkom

    (@artkom)


    Hi, there. I’m using exclude_category() for the main page. I mean I don’t want to show post from “industry” category on the main page
    Unfortunately, in this case, short not working when I’m trying to insert id’s from excluded(“industry”) category.

    Any ideas how to fix it? Thank you so much.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Hello, I started but I think I found a solution for you, tests category_id = “- 52”
    52 is your ID you add the least as in the example.

    By cons me my problem is that I want to do that with several IDs for several categories but it does not work.

    How to do?

    Plugin Author Bill Erickson

    (@billerickson)

    If you would like to exclude multiple categories, it’s best to use a taxonomy query (tutorial).

    [display-posts taxonomy=”category” tax_term=”category-1, category-2″ tax_operator=”NOT IN”]

    Thread Starter artkom

    (@artkom)

    Yes it’s not working when I’m tryind to show posts per ID’s (not categories ID)

    Still not working (

    Plugin Author Bill Erickson

    (@billerickson)

    Can you provide the specific shortcode you’re using that doesn’t work?

    Thanks

    I succeeded thank you here is my code:[display-posts taxonomy=”category” tax_term=”patrice, pierre, ladislas” tax_operator=”NOT IN” category=”LUDOTEK” posts_per_page=”200″]

    http://soludik.fr/liste-ludotheque/

    • This reply was modified 4 years, 11 months ago by darnethil.
    Thread Starter artkom

    (@artkom)

    Here is an example of my shortcode: [display-posts id="5982, 7072, 5542, 31"  image_size="thumbnail" exclude_current="true"]

    The code above is showing only the one post (31). It belongs to the category “vendors”.

    All other posts are not showing (5982, 7072, 5542). These posts are from the “industry” category that was excluded from the main page with exclude_category() function

    I hope it’ll help to sort it out… Please let me know if you need any other information. Thank you

    Plugin Author Bill Erickson

    (@billerickson)

    Can you share the exclude category function? My guess is it’s excluding those posts from all custom queries (like Display Posts) as well.

    Thread Starter artkom

    (@artkom)

    Sorry for the delay. Sure here is it:

    function exclude_category( $query ) {
        if ( $query->is_home() ) {
            $query->set( 'cat', '-135' );
        }
        if ( $query->is_category ) {
            $queried_object = get_queried_object();
            $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );
    
            if ( ! $query->is_admin )
                //exclude the posts in child categories
                $query->set( 'category__not_in', array_merge( $child_cats ) );
        }
    
        return $query;
    }
    Thread Starter artkom

    (@artkom)

    $query->set( 'cat', '-135' );
    This is an id of excluded category. And I want to use post ID’s (NOT category id) for shortcode.
    Thank you

    Plugin Author Bill Erickson

    (@billerickson)

    Yes, that’s the issue right there. Your exclude_category() function affects every query on the site. You need to limit it to the main query using $query->is_main_query().

    Here’s more information: https://www.billerickson.net/customize-the-wordpress-query/

    Thread Starter artkom

    (@artkom)

    Yes, this is the solution.
    The post can be closed as resolved.

    Thank you so much!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘shortcode not working with exclude_category function’ is closed to new replies.