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')
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 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 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/
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