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%')
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 wanted to create a Java application which displays WordPress posts.
So is there any way I can display WordPress posts without wp_query using actual MySQL query?
Adjust the query according your usage.
$querystr = "
SELECT wp_posts.*
FROM wp_posts, wp_postmeta
WHERE wp_posts.ID = wp_postmeta.post_id
AND wp_postmeta.meta_key = 'tag'
AND wp_postmeta.meta_value = 'email'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_posts.post_date < NOW()
ORDER BY wp_posts.post_date DESC
";
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);
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.