wpsharks / wp-kb-articles

KB Articles for WordPress; adds a new Custom Post Type.
http://wpkbarticles.com/
2 stars 1 forks source link

Odd Category Bug #32

Closed jaswrks closed 9 years ago

jaswrks commented 9 years ago

Testing at zencache.com shows a possible bug with category/tax queries under specific conditions that, so far, are hard to determine.

Needs testing.

raamdev commented 9 years ago

Just wanted to note that I recommend disabling caching when testing this to rule that out as a possible culprit. I haven't done this because I wanted you to see what I was seeing, just in case it was a combination of ZenCache + a WPKBA bug.

jaswrks commented 9 years ago

Altering the Questions ⥱ Billing category so that it is no longer a sub-category of Questions resolves the issue for some strange reason.

I'm debugging this at zencache.com now...

Referencing: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Current query vars that are failing to work as expected:

Array
(
    [post_type] => kb_article
    [posts_per_page] => 25
    [paged] => 1
    [orderby] => Array
        (
            [meta_value_num] => DESC
            [comment_count] => DESC
            [date] => DESC
        )

    [meta_key] => wp_kb_articles_popularity
    [meta_query] => Array
        (
            [0] => Array
                (
                    [key] => wp_kb_articles_popularity
                    [compare] => EXISTS
                    [type] => SIGNED
                )

        )

    [ignore_sticky_posts] => 
    [tax_query] => Array
        (
            [relation] => AND
            [0] => Array
                (
                    [taxonomy] => kb_article_category
                    [terms] => Array
                        (
                            [0] => 5
                        )

                    [field] => id
                    [include_children] => 1
                    [operator] => AND
                )

        )

)
raamdev commented 9 years ago
                    [field] => id
                    [include_children] => 1
                    [operator] => AND

Could this be the problem?

I'm just throwing this out there because it came to mind as I was looking over those query vars... is it trying to show all articles that have kb_article_category = 5 AND also have any sub-categories ([include_children] => 1 [operator] => AND)? I.e., there are no articles in Questions → Billing, so it returned no results?

raamdev commented 9 years ago

I.e., there are no articles in Questions → Billing, so it returned no results?

There was an article assigned to the Billing sub-category alone, yes, but it was NOT assigned to Questions AND Billing.

jaswrks commented 9 years ago

Here is the resulting SQL that WP_Query is generating. I'm taking a closer look at this now to see what I can see.

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  (
                SELECT COUNT(1)
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (5,9)
                AND object_id = wp_posts.ID
            ) = 2
) AND wp_posts.post_type = 'kb_article' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND ( 
  wp_postmeta.meta_key = 'wp_kb_articles_popularity' 
  AND 
  ( 
    mt1.meta_key = 'wp_kb_articles_popularity'
  )
) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value+0 DESC, wp_posts.comment_count DESC, wp_posts.post_date DESC LIMIT 0, 25
jaswrks commented 9 years ago
SELECT COUNT(1)
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (5,9)
                AND object_id = wp_posts.ID
            ) = 2

Huh? lol

raamdev commented 9 years ago

Hug? lol

lol :)

jaswrks commented 9 years ago

This should be resolved now. The tax query was trying to satisfy all of the categories, including the children. That's sort of odd how that works. If you pass include_children with operator=AND, WP_Query looks for posts that have all of the parent categories that you give, and it also must have all of the children categories that each of those parents have.

raamdev commented 9 years ago

This should be resolved now.

Awesome! Thank you. Confirmed working now. :)