I've been searching through the past two days to find out where exactly could the Woocommerce products thumbnails (images) texts and URL's are stored inside the database tables, but still cannot figure this out!
I'm in a situation where I must use SQL queries to move the products data into another tables, and I have to implement the process from my phpmyadmin panel.
I already searched the wp_posts and wp_postmeta tables, wp_posts contains a guide column for the url to the product which post_type like 'product%', So far I know that in general the post stores it's thumbnalis link inside one of the posts with a type of attachment , while I need the posts with a post_type of product or like so.
Hope I can find some answers here, Thanks.
You can use the Wordpress WP_Query to get the thumbnail image of product.
$args = array(
'post_type' => 'product', //post type of product
'posts_per_page' => -1
);
$query= new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
global $product;
//woocommerce_get_product_thumbnail is use to get the product thumbnail link
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
Solution 2- using the custom query as you mention in query.
//Using custom query get the details from wp_postmeta and wp_posts
//_wp_attached_file - meta key for image
$querystr = "SELECT p.*, pm2.meta_value as product_image FROM wp_posts p LEFT JOIN
wp_postmeta pm ON (
pm.post_id = p.id
AND pm.meta_value IS NOT NULL
AND pm.meta_key = '_thumbnail_id'
)
LEFT JOIN wp_postmeta pm2 ON (pm.meta_value = pm2.post_id AND pm2.meta_key = '_wp_attached_file'
AND pm2.meta_value IS NOT NULL) WHERE p.post_status='publish' AND p.post_type='product'
ORDER BY p.post_date DESC";
$upload_dir = wp_upload_dir();
//get wp_upload url
$wp_upload_url = $upload_dir['baseurl'];
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ($pageposts as $post){
echo "<br /><a href='".$post->guid."'><img src='". $wp_upload_url.'/'.$post->product_image."'>".$post->post_title."</a>";
}
Related
I have 2 SQL queries as below:
$results2 = $wpdb->get_results("SELECT * FROM `wp_posts` WHERE id IN (".implode(",", $product_ids).")");
$getprice = $wpdb->get_results("SELECT * FROM `wp_postmeta` WHERE meta_key='_regular_price' AND post_id IN (".implode(",", $product_ids).")");
I have combined them as below query:
$yeni = $wpdb->get_results("SELECT wp_posts.id, wp_posts.post_title, wp_postmeta.meta_value
FROM wp_posts
INNER JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id
WHERE meta_key = '_regular_price'
WHERE wp_posts.id IN (".implode(",", $product_ids).")");
After combining the queries, it's giving an empty result. I can't find the issue with the updated query.
Try this sql.
I have replace this WHERE meta_key = '_regular_price'
$yeni = $wpdb->get_results("SELECT wp_posts.id, wp_posts.post_title, wp_postmeta.meta_value FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.id = wp_postmeta.post_id WHERE wp_posts.id IN (".implode(",", $product_ids).") and wp_postmeta.meta_key = '_regular_price'");
I don't understand why you don't use the native Wordpress features via OOP procedure.
Your simple query, it can be translated like this:
<?php
$loop = new WP_Query(
array(
'post_type' => 'page', // post type: page, post, attachment etc..
'post__in' => $productIds, // array[]
)
);
// after that, you can use a standard wordpress loop
if($loop->have_posts()) {
while($loop->have_posts()) {
: $loop->the_post();
the_title();
print "<br>";
// access to meta key _regular_price
print get_post_meta(get_the_ID(), '_regular_price', true);
}
}
This is the right way.
Cheers
Class reference here: https://developer.wordpress.org/reference/classes/wp_query/
I am trying to get ordering working in wp_query, but posts are still being ordered with default settings (just tag__in is working). SQL query for posts looks like this:
string(379) "SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (81) ) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 3"
Here is code snippet:
remove_all_filters('posts_orderby');
$tag = get_term_by('name', 'title_post', 'post_tag');
$args=array(
'order'=>'ID',
'orderby'=>'ASC',
'tag__in' => $tag,
'posts_per_page'=>3, // Number of related posts to display.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
var_dump($my_query->request);
Thanks!
I have checked your code you have to pass wrong arguments.
Can you please check below code?
Wrong
'order'=>'ID',
'orderby'=>'ASC',
Right
'order'=>'ASC',
'orderby'=>'ID',
Ok, I switched order and orderby ...
So, correct arguments are 'orderby'=>'ID','order'=>'ASC',
I am trying to get posts from an external website to show on my own website and I am simply achieving this with the wordpress $wpdb class.
I have managed to display the titles of the posts from the external website on to my website however, I am having trouble displaying the featured image that is associated with it's own post.
Any help would be appreciated.
<?php
$mydb = new wpdb('root','','test','localhost');
$rows = $mydb->get_results(" select post_title, guid FROM wp_posts WHERE post_parent = '21' ");
echo '<ul>';
foreach ($rows as $obj) : ?>
<li><?php echo $obj->post_title; ?></li>
<?php echo $obj->meta_value; ?>
<?php
endforeach;
echo '</ul>';
?>
I managed to get the associated featured image for a post from the external database using the following SQL query below:
"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= %s
AND p1.post_type=%s
ORDER BY
p1.post_date DESC
LIMIT 4", 'publish','post'));
I am having an issue that i couldn't find on google and elsewhere.
To put problem in the context.
I am making a wordpress site where user can put their tutor listing for giving a tutorials, and i am implementing the system of evaluation of the tutors them self and their listening.
So I have tutor_listing post type and each tutor_listing has a 'post_score' meta value, and each tutor (user who puts his tutorials) has user_meta value called 'user_points'.
So I need a WP_Query that will get from the database all published tutor_listing but sorted by (post_score + user_points). Just to emphasis 'post_score' is a post_meta of the post_type tutor_listing and 'user_points' is user_meta of author of that post (tutor_listing).
Also i need that value to show in the loop.
I can get all the tutor_listing by
$args = array(
'post_type' => 'job_listing',
'post_status' => 'publish'
);
$query = new WP_Query( $args );
I can get the result from the custom wordpress sql query like this:
SELECT p1.ID, p1.post_author, p1.post_title, p1.post_date, (um1.meta_value + pm1.meta_value) AS total_score
FROM $wpdb->posts p1
JOIN $wpdb->users u1 ON (p1.post_author = u1.ID)
JOIN $wpdb->usermeta um1 ON (um1.user_id = u1.ID)
JOIN $wpdb->postmeta pm1 ON (pm1.post_id = p1.ID)
WHERE p1.post_type = 'tutor_listing'
AND p1.post_status = 'publish'
AND um1.meta_key = 'user_points'
AND pm1.meta_key = 'post_score'";
Can WP_Query provide such a functionality?
I need WP_Query to use its all extra functionality like pagination ect.
Or is there a way i could all the result done with the my custom sql give to WP_Query object?
Thank you
I need to remove the _thumbnail_id meta key data from a custom post type in my wordpress database. I found this code which will allow me to remove it from everything, but I don't want it to remove it from my posts, just my custom post type called "svg" would anyone be able to help me adjust the code to do so?
global $wpdb;
$attachments = $wpdb->get_results( "
SELECT *
FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
" );
foreach ( $attachments as $attachment ) {
wp_delete_attachment( $attachment->meta_value, true );
}
$wpdb->query( "
DELETE FROM $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
" );
Or if you guys know a better way to remove the _thumbnail metakey from just SVG posts let me know as I have over 400+ svg posts and can't go through each one.
Are you looking for something like this?
DELETE m
FROM wp_postmeta m JOIN wp_posts
ON m.post_id = p.id
WHERE p.post_type = 'svg'
AND meta_key = '_thumbnail_id'
Note: before you try to delete anything make sure that you have solid backup of your database just in case.
First , your code will first delete all attachments ,and then clean (not only clean the _thumbnail_id ) -
Anyhow , in general lines , something like this should work :
SELECT FROM 'wp_posts', WHERE 'post_type' = 'my_post_type'
INNER JOIN 'wp_postmeta' ON 'wp_post.post_id' = 'wp_postmeta.post_id'
AND 'meta_key'='my_meta_key'
// AND 'meta_value'= my_value'// this line only if needed ...
BACKUP YOUR DATABASE .
I take no responsibility for such an action.
Change SELECT with DELETE after verification...
The last line is only for demonstration, in your case I assume you do not need it - so delete accordingly ..