I have the following query:
SELECT wp_posts.id, wp_posts.post_title, wp_postmeta.meta_key, wp_postmeta.meta_value
FROM wp_posts
JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
WHERE (wp_postmeta.meta_key = ? OR wp_postmeta.meta_key = ?)
AND wp_posts.post_type = ?
AND wp_posts.post_status = ?
With this query I want to loop some data about Wordpress posts (not using this query in an WP installation). I want to get 2 values:
(..WHERE (wp_postmeta.meta_key = ? OR wp_postmeta.meta_key = ?)...)
How can I match the second meta_key field result as an new column for the row with the id of the post?
I found a solution..
Tried everything with Pivot, CREATE VIEW, UNION SELECT but this is the perfect solution:
http://subharanjan.com/sql-query-to-get-post-and-its-meta_values-as-one-row-of-record-single-data-grid/
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 want to get wordpress post data and get wp_attached_file from wp_postmeta table
this is my sql query
$query= mysql_query("SELECT wp_posts.ID,wp_posts.post_title,wp_posts.post_date,wp_postmeta.meta_value
FROM wp_posts
JOIN wp_term_relationships
ON wp_term_relationships.object_id = wp_posts.ID
JOIN wp_postmeta
ON wp_postmeta.post_id = wp_posts.ID
WHERE wp_posts.post_date > '$before7'
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'post'
AND wp_term_relationships.term_taxonomy_id = '$cat'
AND wp_postmeta.meta_key = '_wp_attached_file'
ORDER BY wp_posts.post_date DESC LIMIT 10");
it give me nothing but if I removed this line from where clause
AND wp_postmeta.meta_key = '_wp_attached_file'
it works but I need this line to get wp_attached_file
so what is wrong with mysql query
Be sure that the wp_postmeta.meta_key with the value _wp_attached_file really exist and then could be there is some problem for match try trim both cause hidden char
AND trim(wp_postmeta.meta_key) = trim('_wp_attached_file')
In a Wordpress order based system I am selecting people going to an event on a certain date and location.
I also need to see if there are any order notes existing for any matching orders, which I do by selecting the meta_value where the meta_key ="_wc_acof_2".
The problem is when I add the condition to my WHERE clause the code stops returning records where that meta key/value pair doesn't exist (i.e. there are no order notes). Whereas I still need to select those records just return null or similar for the order notes.
The problem line in the WHERE clause is:
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
In my head I would like to write that line similar to:
AND (jn_postmeta_guestnotes.meta_key = "_wc_acof_2" OR 'there is no matching meta_key so just ignore this condition and return the record anyway')
The full query:
SELECT
CONCAT_WS(" ",wp_postmeta.meta_value,jn_postmeta_lastname.meta_value) AS Name,
wp_woocommerce_order_itemmeta.meta_value AS Adults,
jn_postmeta_guestnotes.meta_value AS Notes
FROM
wp_postmeta
LEFT JOIN
wp_postmeta AS jn_postmeta_lastname ON wp_postmeta.post_id = jn_postmeta_lastname.post_id
LEFT JOIN
wp_postmeta AS jn_postmeta_guestnotes ON wp_postmeta.post_id = jn_postmeta_guestnotes.post_id
LEFT JOIN
wp_posts ON wp_postmeta.post_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_items ON wp_woocommerce_order_items.order_id = wp_posts.ID
LEFT JOIN
wp_woocommerce_order_itemmeta ON wp_woocommerce_order_itemmeta.order_item_id = wp_woocommerce_order_items.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_location ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_location.order_item_id
LEFT JOIN
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
WHERE
1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND (jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'" OR jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'")
AND wp_posts.post_status = "wc-completed"
GROUP BY wp_posts.ID
Any advice much appreciated!
The answer is to put the extra bit in a sub-select like so:
(SELECT wp_postmeta.meta_value FROM wp_postmeta WHERE wp_postmeta.meta_key = "_wc_acof_2" AND wp_postmeta.post_id = wp_posts.ID) AS Notes
Just put all filtering condition on the ON so the left join work. On the WHERE will remove the nulls
.....
wp_woocommerce_order_itemmeta AS jn_woocommerce_order_itemmeta_date
ON wp_woocommerce_order_itemmeta.order_item_id = jn_woocommerce_order_itemmeta_date.order_item_id
AND 1 = 1
AND wp_postmeta.meta_key = "_billing_first_name"
AND jn_postmeta_lastname.meta_key = "_billing_last_name"
AND jn_postmeta_guestnotes.meta_key = "_wc_acof_2"
AND wp_woocommerce_order_itemmeta.meta_key = "Adults"
AND jn_woocommerce_order_itemmeta_location.meta_key = "Booking Type"
AND jn_woocommerce_order_itemmeta_location.meta_value LIKE "%'.$showlocation.'%"
AND jn_woocommerce_order_itemmeta_date.meta_key = "Booking Date"
AND
(
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformata.'"
OR
jn_woocommerce_order_itemmeta_date.meta_value = "'.$showdateformatb.'"
)
AND wp_posts.post_status = "wc-completed"
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
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