I have a query that connects to a wordpress db and returns some posts.
In order to get the image for each of these posts i run another query inside a php foreach loop
The problem is that running the second query inside the foreach loop is extremely slow and i need another way to merge these 2 queries into 1.
The first query
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value
IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%')
Based on the returned post ids, i need for each of these ids
the featured image.
This query does this job, but only for 1 id only.
The second query
SELECT wp_posts.guid
FROM wp_posts
WHERE wp_posts.ID =
(Select wp_postmeta.meta_value
FROM wp_postmeta
WHERE wp_postmeta.meta_key =
'_thumbnail_id' AND wp_postmeta.post_id = 'The id of each post')
I need a query to return all the posts alongside with its images.
I really don't understand your data structure, but try this query
select
t1.*, t2.guid
from (
SELECT pm. * , p.*
FROM wp_posts p
JOIN wp_postmeta pm ON pm.post_id = p.ID
WHERE pm.meta_key = 'accommodation_location_post_id'
AND pm.meta_value IN (
SELECT pi.ID
FROM wp_posts pi
WHERE pi.post_title LIKE '%Cyprus%'
)
) t1
left join (
select
p2.guid, pm2.post_id
from
wp_posts p2
join wp_postmeta pm2 on
pm2.meta_value = p2.ID
and pm2.meta_key = '_thumbnail_id'
) t2 on t2.post_id = t1.ID
my query is
SELECT terms.name as artist_name,
wposts.ID,wpostmeta1.meta_value as image
FROM wp_posts wposts
LEFT JOIN wp_postmeta wpostmeta1 ON wposts.ID = wpostmeta1.post_id and wpostmeta1.meta_key = 'fb_thumb'
LEFT JOIN wp_term_relationships ON (wposts.ID = wp_term_relationships.object_id)
LEFT JOIN wp_term_taxonomy term_taxonomy ON (wp_term_relationships.term_taxonomy_id = term_taxonomy.term_taxonomy_id)
LEFT JOIN wp_terms terms ON (terms.term_id = term_taxonomy.term_id)
WHERE term_taxonomy.taxonomy = 'category'
AND term_taxonomy.parent = 1160
AND wposts.post_status = 'publish'
AND wposts.post_type = 'post'
group by terms.name limit 1;
i need to get meta value based on latest post,i was got same terms.name multiple time so i have used group by terms.name but i have problem with meta_value,i am getting first post id meta value instead of latest post id meta value
my attempt was but no luck
LEFT JOIN wp_posts wposts1 ON wposts.ID < wposts1.ID
problem is
how to retrive the last record in each group with wordpress custom query
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
WordPress's wp_postmeta table has all the additional fields for a post but they are in rows so it's easy to add more.
However, now I want to query for all the fields of all the posts lets say, I obviously want those fields in a column and not a row.
This is my query that I am running
SELECT p.post_title,
m.meta_value,
m.meta_key
FROM wp_posts p
JOIN wp_postmeta m
ON p.id = m.post_id
WHERE p.id = 72697;
This will give me all the meta_values and their respective meta keys as columns. But I need the meta keys values as columns and meta values as rows
For example a meta_key could be additional_description and it's value could be What's up
So I need something like this
SELECT p.post_title, additional_description
FROM wp_posts p
JOIN wp_postmeta m
ON p.id = m.post_id
WHERE p.id = 72697;
I need it as a column. I also need all of the posts and not a specific one, but whenever I remove the where it just doesn't query (I have lots of posts, that could be an issue).
Here is some sample data and how I want the results to show up
wp_postmeta table
meta_key post_id meta_key meta_value
1 5 total_related 5
2 5 updated 0
3 5 cricket 1
4 8 total_related 8
5 8 updated 1
6 8 cricket 0
wp_post table
id post_title other things I dont care about
5 This is awesome
8 This is more awesome
wp_post id is related to post_id on wp_postmeta table
Result wanted
post_title total_related updated cricket
This is awesome 5 0 1
This is more awesome 8 1 0
What about something like this?
SELECT p.post_title, m1.meta_value as 'total_related', m2.meta_value as 'updated', m3.meta_value as 'cricket'
FROM wp_posts p
LEFT JOIN wp_postmeta m1
ON p.id = m1.post_id AND m1.meta_key = 'total_related'
LEFT JOIN wp_postmeta m2
ON p.id = m2.post_id AND m2.meta_key = 'updated'
LEFT JOIN wp_postmeta m3
ON p.id = m3.post_id AND m3.meta_key = 'cricket'
And since you aren't looking for a specific post you should be able to do this.
If you want to query specific post_types you can try something like this
SELECT p.post_title, m1.meta_value as 'total_related', m2.meta_value as 'updated', m3.meta_value as 'cricket'
FROM wp_posts p
LEFT JOIN wp_postmeta m1
ON p.id = m1.post_id AND m1.meta_key = 'total_related'
LEFT JOIN wp_postmeta m2
ON p.id = m2.post_id AND m2.meta_key = 'updated'
LEFT JOIN wp_postmeta m3
ON p.id = m3.post_id AND m3.meta_key = 'cricket'
WHERE p.post_type = 'my_custom_post_type';
Try that:
select post_title ,
MAX(CASE WHEN `meta_key`='total_related' THEN meta_value END)as 'total_related',
MAX(CASE WHEN `meta_key` = 'updated' THEN meta_value END) as 'updated' ,
MAX(CASE WHEN `meta_key` = 'cricket' THEN meta_value END) as 'cricket'
FROM wp_posts p
JOIN wp_postmeta m ON p.id = m.post_id
GROUP BY p.id
There are several approaches.
Here's an example of one way to get the specified result, using correlated subqueries in the SELECT list:
SELECT p.post_title
, ( SELECT m1.meta_value
FROM wp_post_metadata m1
WHERE m1.meta_key = 'total_related'
AND m1.post_id = p.id
ORDER BY m1.meta_key LIMIT 1
) AS `total_related`
, ( SELECT m2.meta_value
FROM wp_post_metadata m2
WHERE m2.meta_key = 'updated'
AND m2.post_id = p.id
ORDER BY m2.meta_key LIMIT 1
) AS `updated`
, ( SELECT m3.meta_value
FROM wp_post_metadata m3
WHERE m3.meta_key = 'cricket'
AND m3.post_id = p.id
ORDER BY m3.meta_key LIMIT 1
) AS `cricket`
FROM wp_posts p
WHERE p.id IN (5,8)
There are several other approaches, each with its own advantages and drawbacks.
There's a somewhat related question I referenced in a comment on the question. That question illustrates several approaches, but omits a correlated subquery approach.)
Here's how I did this dynamically - this procedure builds a SQL statement for every postmeta key for a given post type and then runs the "pivot" query for you:
This isn't the fastest query, and we use it only for migration and deep dives into data, but it does the job.
Note that this temporarily resets the max length of the concat function so you can build a large SQL statement:
CREATE PROCEDURE `wp_posts_pivot`(IN post_type_filter varchar(50))
BEGIN
/* allow longer concat */
declare max_len_original INT default 0;
set max_len_original = ##group_concat_max_len;
set ##group_concat_max_len=100000;
SET #sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT CONCAT('MAX(IF(pm.meta_key = ''',
meta_key,
''', pm.meta_value, NULL)) AS `',
meta_key,
'`'))
INTO #sql FROM
wp_posts p
INNER JOIN
wp_postmeta AS pm ON p.id = pm.post_id
WHERE
p.post_type = post_type_filter;
SET #sql = CONCAT('SELECT p.id
, p.post_title
, ', #sql, '
FROM wp_posts p
LEFT JOIN wp_postmeta AS pm
ON p.id = pm.post_id
where p.post_type=\'',post_type_filter,'\'
GROUP BY p.id, p.post_title');
/* reset the default concat */
set ##group_concat_max_len= max_len_original;
/*
select #sql;
*/
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
You can then call this with a simple call such as this one, which will select a single row for each 'page' post type along with all meta values:
call wp_posts_pivot('page');