Combine 2 Queries with different number of rows - php

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

Related

Remove duplicate items from Woocommerce

I had problem during importing products. And now some of the products are duplicated. All of the duplicated products have the same title and attribute (collection_id). Also there are a lot of duplicated media files. Is there a way to remove duplicated products? At least i want to remove products.
I had the same problem and it was I did.
1.- In my database found duplicated products (searching by sku) with the query:
SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
2.- I create a new table called wp_repeated with these skus:
CREATE TABLE wp_repeated AS SELECT meta_value, count(*) AS total FROM wp_postmeta WHERE meta_key = '_sku' GROUP BY meta_value HAVING total > 1
3.- I got the post_id from these skus:
SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)
4.- And, finally, remove and delete from all tables where the post_id appears:
DELETE relations.*, taxes.*, terms.* FROM wp_term_relationships AS relations INNER JOIN wp_term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN wp_terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_postmeta WHERE post_id IN ( SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated)); DELETE FROM wp_posts WHERE ID IN (SELECT p.ID FROM `wp_posts` as p INNER JOIN wp_postmeta as m on p.ID = m.post_id WHERE p.post_type IN ('product','product_variation') AND m.meta_key = '_sku' AND m.meta_value in (select meta_value from wp_repeated));
I hope it can be helpful to you. Regards.
TAKE CARE: these queries remove ALL repeated products.

Fetch data from wordpress DB

I have 2 meta keys which i want attached to particular post type in a single instance, and i created the sql query
SELECT DISTINCT p.ID, p.post_title, pmo.meta_value, pmj.meta_value
FROM wp_posts AS p
RIGHT JOIN wp_postmeta AS pmo ON p.ID = pmo.post_id
RIGHT JOIN wp_postmeta AS pmj ON p.ID = pmj.post_id
WHERE (pmo.meta_key = '_order_meta' AND pmj.meta_key = '_order_jobs')
AND p.post_type = '_order'
AND p.post_status = 'publish'
ORDER BY p.ID DESC LIMIT 10
But when i var dump the data.
it only shows the last 'meta_value', although i am fetching 2 meta_vales

Php mysql search - Search not working if with plural and singular

If I search "sales order" it's fetching "sales order" and "sales orders" in results. It's fetching the result with "s" also.
But if I search "sales orders" it's fetching "sales orders" only but I want "sales order" will also fetch.
I am using php mysql query.
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
Try with this without "%"
p.post_title LIKE 'sales order'
You can also try like this
and (p.post_title LIKE '%sales order%' OR p.post_title LIKE '%sales orders%' )
I hope this will solve your issue.
I am not much sure about this query but you can check as
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales%' AND p.post_title LIKE '%order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
OR
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
and p.post_title LIKE '%sales%order%'
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC
use MySQL Full text search,check if FULLTEXT indexes are there
SELECT DISTINCT * FROM wp_posts as p
inner join wp_postmeta as pm on pm.post_id = p.ID
where (p.post_type = 'abc' or p.post_type = 'xyz')
match (p.post_title) against ('sales order')
or (pm.meta_key = 'xyzkeyword' and pm.meta_value LIKE '%sales order%')
GROUP by p.ID
ORDER BY p.id DESC

how to fetch wp_posts details with source and image

i used the following query to fetch the tittle,data,tagss content,slug,parent id .
this work fine but i need to get the post image and source of image that have a relation ship with wp_postmeta table so i used the second query in while loop inside of first query
First query:-
SELECT wp_posts.ID as Id,wp_posts.post_title as Title,wp_posts.post_date as DATE,
GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
WHERE post_type LIKE 'post' AND post_status LIKE 'publish' AND wp_terms.slug in ('interview','variety','lastpage','sport','arab-global','opinion','business','uae','firstpage')
GROUP BY wp_posts.ID order by post_date limit 150
and the sencond query as
select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['Id']."') = wp_posts.id in ('".$row['Id']."') and meta_key='_wp_attached_file'"
but i does not get any details so i make the relation ship with parent_id of wp_post table as the third query as instead of second query.
select select wp_postmeta.meta_value from wp_postmeta left join wp_posts on wp_postmeta.post_id in ('".$row['parent_id']."') = wp_posts.id in ('".$row['parent_id']."') and meta_key='_wp_attached_file'"
after that i used single query to fetch all records like as below
SELECT wp_posts.ID as Id,wp_posts.post_title as Title,wp_posts.post_date as DATE,wp_postmeta.meta_key,wp_postmeta.meta_value,
GROUP_CONCAT(wp_terms.name) AS TAGS, wp_posts.post_content as CONTENT,wp_terms.term_id,wp_terms.slug,wp_posts.post_parent as parent_id FROM wp_terms
INNER JOIN wp_term_taxonomy ON wp_terms.term_id = wp_term_taxonomy.term_id
INNER JOIN wp_term_relationships ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_posts ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_postmeta ON(wp_posts.ID = wp_postmeta.post_id)
WHERE post_type LIKE 'post' AND post_status LIKE 'publish'
GROUP BY wp_posts.ID order by post_date limit 150
but in that query i got multiple meta_key and multiple meta value there is any useful solution for that so i can fetch wp_post details with its source and image by simple in my sql and php
Try this
$meta_value_id= "select meta_value from wp_postmeta where meta_key='_thumbnail_id' and post_id='".$row['Id']."'";
$query_run_meta= mysql_query($meta_value_id);
$row1= mysql_fetch_assoc($query_run_meta);
$meta_image= "select meta_value from wp_postmeta where meta_key='_wp_attached_file' and post_id='".$row1['meta_value']."'";
$query_meta= mysql_query($meta_image);
$row2= mysql_fetch_assoc($query_meta);
now you can get image source from $row2.
From above query you are getting post id, you can use following functions to get post image url:
$post_thumbnail_id = get_post_thumbnail_id( $post_id ); //It will return id of attached thumbnail
$post_image_src= wp_get_attachment_image_src(post_thumbnail_id);// This will return source url of thumbnail

Select rows as columns for wordpress post meta

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

Categories