0

I have filter plugin on page. And I get generated SQL query by shortcode presets and query string params.

So I want to get SQL query without affecting by query string GET params.

For example. I have page url where I have a filter.

1 example

https : //example.com/snapbacks/

So if I have url like this. WP_Query generates this SQL query

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (38) AND wp_posts.ID NOT IN (
            SELECT object_id
            FROM wp_term_relationships
            WHERE term_taxonomy_id IN (341)
        )
) AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 
'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 16

2 example

If I have url page like this

https : //example.com/snapbacks/?pa_brand=nike

I have this sql query

SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id) WHERE 1=1  AND ( wp_term_relationships.term_taxonomy_id IN (46) AND tt1.term_taxonomy_id IN (38)  AND  wp_posts.ID NOT IN (
            SELECT object_id
            FROM wp_term_relationships
            WHERE term_taxonomy_id IN (341)
        )) AND wp_posts.post_type = 'product' AND ((wp_posts.post_status = 'publish')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 16

So I have a several questions.

  1. How to get sql query showed in first example on page https : //example.com/snapbacks/?pa_brand=nike where "?pa_brand=nike" part added.
  2. How WP_Query generates SQL query? From url or $_GET/$_REQUEST arrays?

1 Answer 1

1

The transition from URL to SQL in WP is quite long and elaborate.

In a very nutshell:

  • URL gets rewritten into a GET request
  • GET request gets converted into query variables
  • query variables are used to instance main query
  • main query generates SQL

There are a lot of nuances to this process, hooks that fire during it, and logic within. This essentially is WordPress, the very core of its core.

Also note that WP_Query might easily generate more than one SQL query, both for logical and performance reasons.

If I follow your question right you have some extension customizing query with its own GET argument. Simply put there is no way to just "tell" WordPress to give you a "clean" query in this case. You would need to study what exact customization is being done and how it should be handled for your purposes, without breaking function it performs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.