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

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

Related

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

split this sql query so it can run on php as it does on workbench

This query runs fine on MySQL workbench but not when I run it on PHP, what am I doing wrong?
$q = "SELECT post_title, post_content 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" AND p1.ID in ('800','808', '569')
ORDER BY p1.post_date DESC";
if ($result = $conn->query($q))
{
while ($row = $result->fetch_assoc())
{
// SHOW three specific posts
printf("Title: %s\r\n",$row['post_title']); echo "<br><hr>";
printf("Content: %s\n",$row['post_content']); echo "<br><hr>";
Since you're creating your string with double quotes, you need to escape all the other double quote in your query:
$q = "SELECT post_title, post_content 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\" AND p1.ID in ('800','808', '569')
ORDER BY p1.post_date DESC";

MYSQL retrieve featured image and post title

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

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 find Woocommerce Products without a Featured Image

I am trying to create a SQL Query that will output the Woocommerce products in my MySQL Database that do not have a featured image assigned to it. As Woo is based on Wordpress, I'm not sure where to get the information from but it looks as if I will need to join some tables up.
Has anyone out there had experience with this? I have 500 products and all need to have a Featured Image. I am going through them randomly and assigning each product one, but I need to see how many are left to do and which ones need to be done.
I am unable to sort the Products by Featured Image in the Woocommerce backend so I am hoping it can be achieved via SQL Query.
It looks as if I have found the right query. It joins some tables to give me a list of all products and featured images. I can then sort this list and see what products have NULL Featured Images. The SQL Query I used is below in case it helps somebody:
SELECT p.ID, p.post_title, pm.*
FROM wp_posts p
LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id
AND pm.meta_key = '_thumbnail_id'
WHERE p.post_type IN ( 'product' )
This would be my query. It will return all post id's ( same as product id's ) that have no image
select ID FROM wp_posts WHERE ID NOT IN (select post_id from wp_postmeta WHERE meta_key="_thumbnail_id") AND post_type="product"
Create a View called Products_List:
SELECT
`wptt_posts`.`post_title` AS `post_title`,
`wptt_posts`.`post_name` AS `post_name`,
`wptt_postmeta`.`meta_value` AS `meta_value`,
`wptt_posts`.`ID` AS `ID`
FROM
(
`wptt_posts`
JOIN `wptt_postmeta` ON (
(
`wptt_posts`.`ID` = `wptt_postmeta`.`post_id`
)
)
)
WHERE
(
(
`wptt_posts`.`post_type` = 'product'
)
AND (
`wptt_postmeta`.`meta_key` = '_sku'
)
)
ORDER BY
`wptt_postmeta`.`meta_value`
Then create another query using Products_List above:
SELECT
Products_List.ID as "Product ID",
Products_List.meta_value as "SKU",
Products_List.post_name as "Product Name",
Products_List.post_title as "Product Title",
'NO' AS "HAS FEATURED Image"
FROM
Products_List
WHERE
ID NOT IN (
SELECT
Products_List.ID
FROM
Products_List
LEFT JOIN wptt_postmeta ON Products_List.ID = wptt_postmeta.post_id
WHERE
wptt_postmeta.meta_key = '_thumbnail_id'
)
UNION
SELECT
Products_List.ID as "Product ID",
Products_List.meta_value as "SKU",
Products_List.post_name as "Product Name",
Products_List.post_title as "Product Title",
'YES' AS "HAS FEATURED Image"
FROM
Products_List
LEFT JOIN wptt_postmeta ON Products_List.ID = wptt_postmeta.post_id
WHERE
wptt_postmeta.meta_key = '_thumbnail_id'
Another possibility that does not use a NOT IN :
SELECT p.ID, p.post_title
FROM `wp_posts` as p LEFT OUTER JOIN wp_postmeta pm ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
WHERE p.post_type = 'product'
AND
(meta_key IS NULL OR meta_value = "")
is possible whith sku ? ( no only id)
SELECT p.ID, p.post_title
FROM `wp_posts` as p
LEFT OUTER JOIN wp_postmeta pm
ON (p.ID=pm.post_id AND pm.meta_key = '_thumbnail_id')
WHERE
p.post_type = 'product'
AND (meta_key IS NULL OR meta_value = "")
Simple as that. The following sql returns sku of all products with the default woocommerce image (means all products without image):
select wp_postmeta.meta_value from wp_postmeta where wp_postmeta.post_id IN (select ID FROM wp_posts WHERE ID NOT IN (select post_id from wp_postmeta WHERE meta_key="_thumbnail_id") AND post_type="product" AND post_status!='auto-draft') AND wp_postmeta.meta_key = "_sku";

Categories