My site has installed woocommerce. When I visit taxonomy page, the below SQL is executed
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (90) )
AND wp_posts.post_type IN ('post', 'page', 'product')
AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'expired' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private')
AND ( (wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog')) )
AND wp_posts.post_password = ''
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_title ASC
LIMIT 0, 10
Can I remove the following where clause
AND ( (wp_postmeta.meta_key = '_visibility' AND CAST(wp_postmeta.meta_value AS CHAR) IN ('visible','catalog')) )
The reason I wanna do this is because I want to display post & page results together.
UPDATE 1
The SQL statement above only display product result only as there is a WHERE clause which filter out post and page result. Therefore, I want to know if there is any hook to remove that particular WHERE clause statement so that the result includes post, page and product.
Related
I can get it working for posts that match a title and are in a category, however I cannot get posts that match a title and are NOT in a category
select *
from wp_posts
join wp_term_relationships on (wp_posts.ID = wp_term_relationships.object_id)
where (wp_term_relationships.term_taxonomy_id NOT in (107))
and (post_title REGEXP 'video|film' )
and (post_type = 'post' OR post_type = 'xdays1')
GROUP BY wp_posts.ID
It has the same amount of results as this, without any category code:
select *
from wp_posts
WHERE
(post_title REGEXP 'video|film' )
and (post_type = 'post' OR post_type = 'xdays1')
GROUP BY wp_posts.ID
I assume my syntax is wrong...
I have about 200 posts in category with id 107. So I want results to not include those.
help appreciated!
I don't think you need to use "join". Try the following code instead, see if you could get it to work.
SELECT wp_posts.*
FROM wp_posts
WHERE (wp_posts.post_title REGEXP 'video|film' )
AND ( wp_posts.ID NOT IN ( SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id IN (107) ) )
AND (wp_posts.post_type = 'post' OR wp_posts.post_type = 'xdays1')
AND wp_posts.post_status = 'publish'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
I'm trying to figure out why this query returns all post types and not just the ones specified. Any help would be helpful.
SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_terms, wp_term_taxonomy
WHERE 1=1
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_taxonomy.term_id
AND wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
AND wp_posts.post_type IN ('insight')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_status != 'private'
AND wp_posts.post_status != 'future'
AND wp_posts.post_status != 'trash'
AND wp_terms.slug LIKE '%great%'
OR wp_posts.post_content LIKE '%great%'
LIMIT 0, 10
Your problem probably it's in your OR, you should use the OR inside parentheses (). As you are doing, the query will ignore all your clauses before. Try this:
AND ( wp_terms.slug LIKE '%great%' OR wp_posts.post_content LIKE '%great%' )
I have created a custom post type and taxonomy for a goods catalogue for a hire website. This works fine. I am trying to generate a report that lists all items and their category and sub category. I came up with this to generate a table with the main category:
SELECT
wp_posts.ID as ID,
wp_posts.post_title as post_title,
wp_terms.name as main_category,
wp_terms.term_id as main_category_id
FROM
wp_posts, wp_term_relationships, wp_terms, wp_term_taxonomy
WHERE
wp_term_relationships.object_id = wp_posts.ID
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id
AND wp_term_taxonomy.parent = '0'
AND wp_posts.post_type = 'goods'
AND wp_posts.post_status = 'publish'
ORDER BY wp_terms.term_id ASC
I just loop through and its fine. But I want to show the sub category too - which I can do by putting another query in the foreach php loop.
BUT, I need to order by main category and then sub category.
So I figured I need it all in one query. And it must be possible. I have tried experimenting with JOINing tables in the query but can't get the syntax right. Heres the latest one I tried.
SELECT
wp_posts.ID as ID,
wp_posts.post_title as post_title,
s1.name as main_category,
s1.term_id as main_category_id,
s2.name as main_category,
s2.term_id as main_category_id
FROM
wp_posts, wp_term_relationships, wp_terms s1, wp_term_taxonomy
LEFT JOIN wp_terms s2
ON s1.term_id = s2.id
WHERE
wp_term_relationships.object_id = wp_posts.ID
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = s1.term_id
AND wp_term_taxonomy.parent = '0'
AND wp_posts.post_type = 'goods'
AND wp_posts.post_status = 'publish'
ORDER BY s1.term_id ASC
I've stored a bunch of post meta data inside a serialized array and I'm trying to write a custom SQL query that will only retrieve the results that are upcoming:
I'm currently at the following but It doesn't seem to want to get the results.
MY outputted SQL is:
SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.post_type = 'cjd' AND ((wp_posts.post_status = 'publish')) AND ( ( wp_postmeta.meta_key = 'cjd_data' AND CAST(wp_postmeta.meta_value AS SIGNED) >= 'date' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC
And my functionality is:
$wpdb->query(
$wpdb->prepare( "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) WHERE 1=1 AND wp_posts.post_type = 'cjd' AND ((wp_posts.post_status = 'publish')) AND ( ( wp_postmeta.meta_key = 'cjd_data' AND CAST(wp_postmeta.meta_value AS SIGNED) >= 'date' ) ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC")
);
I've tried everything and I can't use meta_query on a custom $args as the date field is inside the serialized array.
I'm running a site on Wordpress and I'm trying to get information from the postmeta table based on 2 (or more) fields. Here is my query so far:
SELECT wp_postmeta.* FROM wp_postmeta
LEFT JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id
WHERE wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND ( wp_postmeta.meta_key = 'relevantLine' AND wp_postmeta.meta_value = '339' )
AND (
( wp_postmeta.meta_key = 'brandOne' AND wp_postmeta.meta_value = '30' )
OR ( wp_postmeta.meta_key = 'brandTwo' AND wp_postmeta.meta_value = '30' )
OR ( wp_postmeta.meta_key = 'brandThree' AND wp_postmeta.meta_value = '30' )
)
AND wp_posts.post_date >= '2014-03-25'
AND wp_posts.post_date <= '2014-11-27'
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
I'm trying to access the records that have the postmeta key "relevantLine" set to 339 AND the postmeta key "brandOne" set to 30 (or "brandTwo" set to 30, or "brandThree" set to 30).
Does anyone have any idea how to do this?
The above query isn't working.
Many thanks
PS. I know I could use the wp query functionality but I would like to run the query this way if possible.
You can rewrite your as below
SELECT m.* ,m1.*
FROM wp_postmeta m
JOIN wp_posts p ON p.ID = m.post_id
JOIN wp_postmeta m1 ON p.ID = m1.post_id
WHERE p.post_status = 'publish'
AND p.post_type = 'post'
AND m.meta_key = 'relevantLine' AND m.meta_value = '339'
AND m1.meta_key IN ('brandOne','brandThree','brandTwo')
AND m1.meta_value = '30'
AND p.post_date >= '2014-03-25'
AND p.post_date <= '2014-11-27'
GROUP BY p.ID
ORDER BY p.post_date DESC
This structure is call EAV entity attribute value and for matching between different keys you have to join the table as the different keys you want to compare,I have added only one join to wp_postmeta and for the keys of single value that is 30 i have used IN() to simplify your query.
Note Using GROUP BY without any aggregate function will give you
indeterminate results