• Resolved anomalocaris

    (@anomalocaris)


    Hi,

    I have a question that I think will have a similar solution to one asked in this thread but I haven’t worked out how to apply the info.

    I am using your widget to display Products (WooCommerce) in my sidebar. As far as I can tell, based on the above thread, the categories and tags applied to Products will need to be specified via the Custom Taxonomies. However, I can’t tell how the Custom Taxonomies work, exactly. I’ve tried a variety of things and usually end up with “No Posts Yet” as the result.

    I currently have two categories of items that I’d like to have show in the sidebar (with thumbnails and a short description). I can specify Products either by those categories *and* as items that don’t have a specific tag (“back-issue”) or I can just have it display all products that don’t have the tag “back-issue”. Either one should work for my purposes.

    I suspect I am just not entering the terms correctly.

    I ran the debugger and got the following for the query for the widget:
    $pis_query = Array
    (
    [post_type] => product
    [tax_query] => Array
    (
    [0] => Array
    (
    [taxonomy] => back-issue
    [field] => slug
    [terms] => Array
    (
    [0] => back-issue
    )

    [operator] => NOT IN
    )

    )

    [posts_per_page] => 2
    [orderby] => date
    [order] => DESC
    [post_status] => publish
    )

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Aldo Latino

    (@aldolat)

    Hi,
    the query should be corrected a little.

    In your case you want to display any published post that is not under back-issue term of taxonomy. First of all: what’s the name of this taxonomy? For example, “category” is the name of a taxonomy, like post-tag is the name of another taxonomy. In the code you entered, the name of the taxonomy is missing:

    [taxonomy] => back-issue <<<=== This should be the name of the taxonomy
    ...
    [0] => back-issue

    So, let’s see an example.

    In my site I want to display posts from the custom post type product. I do not want to display products that have an HD display.

    In this case:
    display is the name of a taxonomy
    hd is a term of the taxonomy display.

    The query will be:

    $pis_query = Array
    (
        [post_type] => product
        [tax_query] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => display
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => hd
                            )
    
                        [operator] => NOT IN
                    )
    
            )
    
        [posts_per_page] => 6
        [orderby] => date
        [order] => DESC
        [post_status] => publish
    )

    The following are two screenshots​ of the widget panel:

    Let me know, please.

    • This reply was modified 9 years, 1 month ago by Aldo Latino.
    Thread Starter anomalocaris

    (@anomalocaris)

    I think I executed this correctly (thanks for the very clear explanation) but I am still not getting anything but “no posts yet.” I am not sure if I have perhaps not gotten the taxonomies set up correctly to begin with, or if I am still inputting things incorrectly.

    My terms are set up as categories (‘issue’ and ‘supplement’) and tags (‘back-issue’)

    Debug results following what I think was the correct implementation based on your explanation:

    
    <code>
    "$pis_query = Array
    (
        [post_type] => product
        [tax_query] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => issue, supplement
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => back-issue
                            )
    
                        [operator] => NOT IN
                    )
    
            )
    
        [posts_per_page] => 2
        [orderby] => date
        [order] => DESC
        [post_status] => publish
    )"
    </code>
    
    • This reply was modified 9 years, 1 month ago by anomalocaris.
    • This reply was modified 9 years, 1 month ago by anomalocaris. Reason: formatting issue
    Plugin Author Aldo Latino

    (@aldolat)

    In the query, this line

    
    [taxonomy] => issue, supplement
    

    must contain the name of the taxonomy, not the terms of the taxonomy.
    Knowing that:

    issue and supplement are categories;
    back-issue is a tag;
    – you want to display any product (a custom post type), regardless of the category;
    – you want to hide posts tagged with back-issue

    here is the correct query:

    $pis_query = Array
    (
        [post_type] => product
        [tax_query] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => post_tag
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => back-issue
                            )
    
                        [operator] => NOT IN
                    )
    
            )
    
        [posts_per_page] => 2
        [orderby] => date
        [order] => DESC
        [post_status] => publish
    )

    This query will show the most recent two posts published as product, excluding those tagged with back-issue.

    Here is the updated screenshot:

    Let me know, please.

    P.S. After solving this step, I will tell you how to get posts with certain categories and exclude posts tagged with certain tags.

    Thread Starter anomalocaris

    (@anomalocaris)

    Bingo–worked perfectly. Thank you! I figured I was just not getting a detail and that was screwing everything up.

    Looking forward to the explanation about getting posts with certain categories and excluding posts tagged with certain tags. I am guessing it will involve the second column A2 or the B column.

    • This reply was modified 9 years, 1 month ago by anomalocaris.
    Plugin Author Aldo Latino

    (@aldolat)

    Yes, you’re right.

    Let’s say that we have a custom post type “product”. We have already published products under many categories and tags. We want to display products published under the “aciform” category, excluding those tagged with “habergeon”.

    Since you have already understood the solution, an image should be enough:

    This is the resulting query:

    $pis_query = Array
    (
        [post_type] => product
        [tax_query] => Array
            (
                [relation] => AND
                [0] => Array
                    (
                        [taxonomy] => category
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => aciform
                            )
    
                        [operator] => IN
                    )
    
                [1] => Array
                    (
                        [taxonomy] => post_tag
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => habergeon
                            )
    
                        [operator] => NOT IN
                    )
    
            )
    
        [posts_per_page] => 6
        [orderby] => date
        [order] => DESC
        [post_status] => publish
    )

    Obviously you can enter more comma-separated categories and/or tags.

    In case of custom taxonomies, enter the taxonomy name instead of category and the term name instead of post_tag.

    Have fun!

    Thread Starter anomalocaris

    (@anomalocaris)

    Thanks so much! I am, indeed, looking forward to playing with this.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Custom Categories Question’ is closed to new replies.