0

I have a query object, where both tax_query and p parameters are set

[query] => Array
    (
        [tax_query] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => destination_tag
                        [field] => slug
                        [terms] => Array
                            (
                                [0] => country
                            )

                    )

            )

        [post_type] => dc-location
        [lang] => en
        [posts_per_page] => 10
        [paged] => 1
        [p] => 7
    )

The resulting SQL query ignores the tax_query parameter

[request] => SELECT   wptests_posts.* FROM wptests_posts  WHERE 1=1  AND wptests_posts.ID = 7 AND wptests_posts.post_type = 'dc-location'  ORDER BY wptests_posts.post_date DESC

If I remove the p parameter, the query includes the tax query correctly. Is there a way to force the sql to parse tax_query even if p is defined?

  • 2
    Have you tried post__in instead of p? Why are you trying to combine them anyway? What's your goal? – swissspidy Mar 26 '18 at 13:59
  • @swissspidy the goal is to return a post only if it has a taxonomy term. It's an API design constraint. post_in works as expected, put the comment as an answer and I will accept it, I will open a track ticket on WP to understand if this is the desired behavior, it should at least be documented – Nicola Peluchetti Mar 26 '18 at 14:03
3

p is a special argument in WP_Query in that it short-circuits most of the query parsing logic to request a single post.

In your case, I'd suggest using the post__in argument when you need to use tax_query at the same time.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.