I'm trying to write a custom select post query for wordpress. I'm using woocommerce. In woocommerce the default post type name is "product" and taxonomy name in "product_cat"
I've added another taxonomy names "spec" for product specification.
So in my product category assume "samsung", "laptop" is a term name for product category. and in "spec" I've some specification such as its "core i5 processor", "8gb ddr3 ram" etc. and my post title is "super notebook"
I want to search it for any of this field like if type notebook in my search field it will show all notebook. Underneath the search input type text area I also have a select box which has all "product_cat" there like samsung, hp, apple, laptop etc.
Product title / Post title
Super notebook
product_cat
samsung
laptop
spec
core i5 processor
8gb ddr3 ram
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
I try this code it show the result.
But if I try this it'll not show the result
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
AND wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
I don't have any idea to fix this. So anyone want to help me please give this answer for me.
Thanks.
You can try this
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
Related
I used below code to select products from specific categories and works fine but I want to select products if include more than one category
SELECT post.ID, post.post_title FROM `wp_posts` as post
INNER JOIN wp_term_relationships AS tr ON tr.object_id = post.ID
WHERE
post.`post_type` IN ('product','product_variation')
AND tr.term_taxonomy_id IN(32,25)
I use IN(32,25) and it returns all products, how can I filter products just included in two categories?
To query products that are in specific categories (e.g. categories with the ids of 32 and 35), you could use this:
SELECT wp_posts.* 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 (32,35) )
AND
wp_posts.post_type = 'product'
AND
(wp_posts.post_status = 'publish')
GROUP BY
wp_posts.ID
ORDER BY
wp_posts.post_date DESC
It's recommended to use global $wpdb and take advantage of
$wpdb->prefix for your wordpress table "prefix", instead if hard coding "wp_"
and
$wpdb->prepare for security.
Like this:
global $wpdb;
$query = $wpdb->prepare(
"SELECT {$wpdb->prefix}posts.* FROM {$wpdb->prefix}posts LEFT JOIN {$wpdb->prefix}term_relationships
ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id)
WHERE 1=1
AND
( {$wpdb->prefix}term_relationships.term_taxonomy_id IN (32,35) )
AND
{$wpdb->prefix}posts.post_type = 'product'
AND
({$wpdb->prefix}posts.post_status = 'publish')
GROUP BY
{$wpdb->prefix}posts.ID
ORDER BY
{$wpdb->prefix}posts.post_date DESC"
);
$sql_results = $wpdb->get_results($query, ARRAY_A);
For security reasons, avoid writing your own sql queries as much as possible.
In order to query your database try to use:
wp_queryDocs
or
wc_get_productDocs
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 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
Okay, trying to pull off some ninja development here, but getting a little stuck. I require the help of a true expert here. Thanks in advance.
This Query works excellent to return post information and category from outside of Wordpress:
SELECT wp_posts.ID,wp_posts.post_title,wp_terms.name as category FROM wp_posts
INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_status = 'publish'
Now, this query works great for filtering by the company name, which is a meta field stored in the postmeta table. (The {$q} below is the sanitized company name)
SELECT ID,post_title,post_date,post_name,post_author FROM wp_posts,wp_postmeta
WHERE ID = wp_postmeta.post_id AND meta_key = 'company'
AND meta_value = \"{$q}\" AND wp_posts.post_status = 'publish'
ORDER BY post_date DESC
Anyway, both queries work great on their own. However, I want to combine them into one query so that I can select the category but still filter by company.
Any thoughts? I would love to hear your ideas on how to solve this problem. I'd give you 3x points for this answer if I could.
One way to accomplish this:
SELECT wp_posts.ID,wp_posts.post_title,wp_terms.name as vert FROM wp_posts
INNER JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_term_taxonomy on wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
INNER JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_status = 'publish'
AND wp_posts.ID IN (SELECT post_id FROM wp_postmeta WHERE meta_key = 'company' AND meta_value = \"{$q}\" ) LIMIT 0,99999
This mySQL query searches for items in a wordpress database looking for similarities in the title.
SELECT ID, post_name, post_title, post_date
FROM wp_posts
WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' AND wp_posts.post_title LIKE '%$my_text%' ORDER BY wp_posts.post_date DESC
I have expanded it to include tags as well.
SELECT ID, post_name, post_title, post_date
FROM wp_posts
LEFT JOIN wp_term_relationships
ON object_id = wp_posts.id
LEFT JOIN wp_terms
ON term_id = wp_term_relationships.term_taxonomy_id
WHERE wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' AND wp_posts.post_title LIKE '%$my_text%'
OR wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish' AND wp_terms.name LIKE '%$my_text%'
This too works but it adding to the query the number of times a post can be fetched. For example, if we type in a search term that appears only in the title, but the same post ID is in the tag table for four unrelated tags, it fetches the post from the title similarity four times not one. Obviously this query needs some tweaking but I am very new to mySQL and need a hand.
Here is a helpful link for UNION selects: http://www.mysqltutorial.org/sql-union-mysql.aspx
... WHERE
wp_posts.post_type = 'post' AND wp_posts.post_status = 'publish'
AND (wp_posts.post_title LIKE '%$my_text%' OR wp_terms.name LIKE '%$my_text%')