MYSQL retrieve featured image and post title - php

I would like to retrieve a thumbnail version of the "featured image"
this is the code I have but it is pulling the large image.
SELECT a.post_title title, max(c.guid) img_url, a.ID id, a.post_name
FROM wp_posts a
LEFT JOIN
(select post_parent, max(post_date_gmt) as latest_image_date
from wp_posts
where post_type='attachment'
GROUP BY post_parent) b
on a.id=b.post_parent
LEFT JOIN
wp_posts c
on c.post_parent=a.id
and c.post_type='attachment'
and b.latest_image_date = c.post_date_gmt
WHERE c.guid IS NOT NULL
GROUP BY a.post_title
ORDER BY a.ID

May be this query works for U. It's working absolutely fine for me.
SELECT p1.*, wm2.meta_value
FROM wp_posts p1
LEFT JOIN
wp_postmeta wm1 ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wp_postmeta wm2 ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
LEFT JOIN
wp_term_relationships wtr ON (
object_id=p1.id
)
WHERE
p1.post_status='publish'
AND p1.post_type='post'
AND `term_taxonomy_id`=?
ORDER BY p1.post_date DESC
LIMIT 0,10

Related

Wordpress delete posts not in specific categories using wpdb query

How do I modify the following query to delete all posts with the custom post type "listings" that are NOT IN specific Wordpress categories?
Note, I must use $wpdb->query() in my particular situation.
My categories for exclusion are term ID's 21, 22, and 24.
$wpdb->query('DELETE FROM wp_posts WHERE post_type = "listings"');
UPDATE - THIS query brings me a lot closer to what I am after but throws a SQL error.
DELETE FROM wp_posts a
LEFT JOIN wp_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN wp_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN wp_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN wp_terms e ON ( e.term_id = d.term_id )
WHERE a.post_type = "listings"
AND e.term_id NOT IN (21,22,24);
Error:
WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id' at line 1 for query DELETE FROM wp_posts LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_taxonomy.term_id WHERE wp_posts.post_type = "listings" AND wp_terms.term_id NOT IN (21,22,24)
$wpdb->query('DELETE wp_posts 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.object_id = wp_term_taxonomy.term_taxonomy_id
WHERE wp_posts.post_type = "listings" AND wp_term_taxonomy.taxonomy NOT IN (taxonomy_1_slug, taxonomy_2_slug, etc.)');
Using category id delete post
delete a,b,c,d
FROM esx_posts a
LEFT JOIN esx_term_relationships b ON ( a.ID = b.object_id )
LEFT JOIN esx_postmeta c ON ( a.ID = c.post_id )
LEFT JOIN esx_term_taxonomy d ON ( d.term_taxonomy_id = b.term_taxonomy_id )
LEFT JOIN esx_terms e ON ( e.term_id = d.term_id )
WHERE e.term_id = 48

Retrieve Wordpress post with featured image/thumbnail in one sql query

here's my code :
SELECT p.ID, p.post_title,
MAX(IF(pm.meta_key = 'featured_image', pm.meta_value, NULL)) AS event_imgID
FROM wpgp_posts AS p
LEFT JOIN wpgp_postmeta AS pm on pm.post_id = p.ID
WHERE p.post_type = 'product' and p.post_status = 'publish'
GROUP BY p.ID
event_imgID retrieve the ID from postmeta but the img url is stored in the wpgp_posts in the guid column
How to achieve this ?
You can join in the table to get more information about the image:
SELECT . . .
FROM (SELECT p.ID, p.post_title,
MAX(CASE WHEN pm.meta_key = 'featured_image' THEN pm.meta_value END) AS event_imgID
FROM wpgp_posts p LEFT JOIN
wpgp_postmeta pm
ON pm.post_id = p.ID
WHERE p.post_type = 'product' and p.post_status = 'publish'
GROUP BY p.ID
) pi LEFT JOIN
wpgp_posts wp
ON pi.event_imgID = wp.guid
I finally found the way to do it. I had to search thumbnail_id instead of guid :
SELECT
p1.ID, p1.post_title,
MAX(IF(pm.meta_key = 'listing_event_date', pm.meta_value, NULL)) AS event_date,
wm2.meta_value as event_img
FROM
wpgp_posts p1
LEFT JOIN wpgp_postmeta AS pm on (pm.post_id = p1.ID)
LEFT JOIN
wpgp_postmeta wm1
ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wpgp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
WHERE
p1.post_status='publish'
AND p1.post_type='product'
GROUP BY p1.ID, wm2.meta_id
ORDER BY
event_date ASC
Thanks

Wordpress: How to get posts that don't contain a specific category with a custom query

I need to display all posts that don't contain a specific category.
I use the following query, but I retrive also the post that contains the category 81 since this post contains also other categories.
Is there a way to sort it out?
SELECT p1.*, wm2.meta_value
FROM wp_posts p1
LEFT JOIN wp_postmeta wm1 ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
LEFT JOIN
wp_term_relationships wtr
ON (
object_id=p1.id
)
WHERE
p1.post_status='publish'
AND p1.post_type='post'
AND `term_taxonomy_id`<>81
GROUP BY ID
ORDER BY p1.post_date DESC
LIMIT 0,10
Thank you!
This should works
SELECT p1.*, wm2.meta_value
FROM wp_posts p1
LEFT JOIN wp_postmeta wm1 ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = '_thumbnail_id'
)
LEFT JOIN
wp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = '_wp_attached_file'
AND wm2.meta_value IS NOT NULL
)
LEFT JOIN
wp_term_relationships wtr
ON (
object_id=p1.id
)
WHERE
p1.post_status='publish'
AND p1.post_type='post'
AND object_id NOT IN (SELECT `object_id` FROM wp_term_relationships where `term_taxonomy_id`=81)
GROUP BY ID
ORDER BY p1.post_date DESC
LIMIT 0,10

How to get post thumbnail URL manually in wordpress plugin development?

I want to get post thumbnail url manually without using this code
wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
For example using MySQL query.Its possible?
Try This MySql Query
SELECT
p1.*,
wm2.meta_value
FROM
wp_posts p1
LEFT JOIN
wp_postmeta wm1
ON (
wm1.post_id = p1.id
AND wm1.meta_value IS NOT NULL
AND wm1.meta_key = "_thumbnail_id"
)
LEFT JOIN
wp_postmeta wm2
ON (
wm1.meta_value = wm2.post_id
AND wm2.meta_key = "_wp_attached_file"
AND wm2.meta_value IS NOT NULL
)
WHERE
p1.post_status="publish"
AND p1.post_type="post"
ORDER BY
p1.post_date DESC

(mysql) Get woocommerce Order by meta_key and product_id

I have a mysql issues.
I have this select
SELECT DISTINCT t2.meta_key,t2.meta_value
FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id
JOIN wp_posts as t3 ON t3.ID=t1.order_id
WHERE t2.order_item_id IN(SELECT distinct t1.order_item_id FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id WHERE t2.meta_key like "_product_id" AND t2.meta_value = 99) AND t2.meta_key="member_id" AND t3.post_status like "publish"
this Select query get all "member_id"(meta_key) where the "_product_id" equals 99.
I hav3 a good result. it's maybe not "elegant" but it's works.
My problem now, I need to get all member_id where the _product_id equal 99 AND
the Order is not cancelled.
Also, I don't know which table and what is the real key that "order_status" are.
So,
Thanks For you help!
EDIT
I added the
JOIN wp_posts as t3 ON t3.ID=t1.order_id
and
WHERE t3.post_status = "publish"
The current value that I need is in the table: wp_term.
thanks
ok, I got it.
This query take all "meta_value" (here: member_id)
On all orders where
Order status is not : "failed' 'cancelled' or 'refunded'
where order have the product id is 99
Where the order is not into the trash.
so :
SELECT t2.meta_value
FROM wp_woocommerce_order_items AS t1
JOIN wp_woocommerce_order_itemmeta AS t2 ON t1.order_item_id = t2.order_item_id
WHERE t2.order_item_id IN(SELECT DISTINCT oi.order_item_id
FROM wp_posts, wp_postmeta, wp_term_relationships tr,wp_term_taxonomy tt,
wp_terms te, wp_woocommerce_order_itemmeta oi, wp_woocommerce_order_items ii
WHERE tt.taxonomy like "shop_order_status"
AND te.term_id = tt.term_id
AND te.slug NOT IN ('failed','cancelled','refunded')
AND tr.term_taxonomy_id = tt.term_taxonomy_id
AND oi.order_item_id = ii.order_item_id
AND oi.meta_key = '_product_id'
AND oi.meta_value = 99
AND ii.order_id = wp_posts.ID
AND wp_postmeta.post_id = tr.object_id
AND wp_posts.post_status = 'publish') AND t2.meta_key like "member_id"
I hope to help someOne.
If you have more elegant or more optimisation of this query.
Feel free to tell us :-)

Categories