I have been trying to fetch posts from a wordpress site's mysql db to my own script for a few days.
<?php
$querystr = "
SELECT DISTINCT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->postmeta.meta_key = 'tag'
AND $wpdb->postmeta.meta_value = 'email'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_date < NOW()
ORDER BY $wpdb->posts.post_date DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
I can see thumbnail images on the original site but I could not figure out where to get that image location data in the db.
What would be the enhancements on my query in order to have image information with current ones?
You need to dive into your wp_postmeta table. This is where you will find everything. You can get the thumbnail ID with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**ID_HERE** AND meta_key='_thumbnail_id'
You will get a thumbnail ID from which you will be able to get your real thumbnail with the following query:
SELECT * FROM 'wp_postmeta' WHERE post_id=**PREVIOUSLY_OBTAINED_ID**
This will actually give you two rows :
One for the URL of your image (meta_key="_wp_attached_file")
One for the image info like its dimensions, its title etc... (meta_key="_wp_attachment_metadata")
Try this code:
<?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
More details here: http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail
Related
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>";
}
I've been trying to find a way (armed with only a basic knowledge of php and a wildly optimistic sense of determination) to search a wordpress database for meta-values based on other meta-values.
I need to get an item name from one table (wp_posts) using the post's ID, use it to obtain the post_id of items with the same name in another table (wp_postmeta) and then get from the same table the values for a particular custom field for every post_id obtained.
Since I'm new to this I attempted to do it by breaking it down into parts and here's what I've got.
<!-- This part works and gets the post title as expected -->
<?php
$result1 =$wpdb->get_results("SELECT post_title FROM $wpdb->posts , $wpdb->postmeta where $wpdb->posts.ID = $post->ID", OBJECT);
print_r($result1[0]->post_title);
?>
<!-- Then, I expected this to return the post_id of all entries where the title is the same as the one just obtained and where meta_key = custom_field_one. But it doesn't -->
<?php
$result2 =$wpdb->get_results("SELECT post_id FROM $wpdb->posts , $wpdb->postmeta where $wpdb->postmeta.meta_value = $result1 AND $wpdb->postmeta.meta_key = custom_field_one", OBJECT );
print_r($result2->post_id);
?>
<!-- So, unsurprisingly, this part doesn't work either. But it should use every post_id it just obtained to once again search the postmeta table -->
<?php
$result3 =$wpdb->get_results("SELECT meta_value FROM $wpdb->posts , $wpdb->postmeta where $wpdb->postmeta.post_id = $result2 AND $wpdb->postmeta.meta_key = custom_field_two", OBJECT );
print_r($result3->meta_value);
?>
Since the first query works, and the subsequent ones are just slight variations on that I expected it all to work. But, well, I was wrong.
Incidentally, I've tried a number of syntax variations in case that was an issue and I'm aware there are some issues regarding arrays. So any advice would be appreciated.
Use aliases:
SELECT
a1.Name,
b1.Info
FROM table2 b1
JOIN table2 a1 ON b1.id= a1.id AND a1.status = 1
I want a select box that allows users to pick the order of posts displayed. (Like youtube order by date / relevance)
I'm displaying a list of custom post types (products). I want users to be able to select the order by price & size. (these are both custom fields).
I've written this code which allows me to change the order by changing the default variables below.
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $wpdb->postmeta.meta_value $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
I now need to let public change the default variables with a select box, but I don't know how.
<form method="post">
<select name="custom_field_choice">
<option value="size">Size</option>
<option value="price">Price</option>
</select>
<input type="submit" value="order_select" />
</form>
Is it possible to allow a user to change a php variable with a select box? If not, whats the best way to do this?
Try to change your code to receive variable from http post request like this
// Default variables
$post_type = 'products';
$custom_field = 'size';
$order = ASC; // ASC or DESC
$orderField = $post->custom_field_choice;
// Find all matching posts in wordpress database
$querystr = "
SELECT $wpdb->posts.*
FROM $wpdb->posts, $wpdb->postmeta
WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = '$post_type'
AND $wpdb->postmeta.meta_key = '$custom_field'
ORDER BY $orderField $order
";
// Get all the posts
$pageposts = $wpdb->get_results($querystr, OBJECT);
Option's values size and price must be column name on your database table.
I have an old function which uses the post excerpt to hold the image thumbnail. It was a bit hackey and worked for a long time.
Now I need to use the post excerpt for, you know, an excerpt. So I'm looking to update this function to grab the image src info straight from the post attachments instead.
The question:
How can I update the $before_sql SQL code below to grab the first attached image in the post attachment?
The code:
(I think I am only concerned about the sql portion as the rest should clean itself up?) There's more code section too, but instead of pasting ALL of it here, this snippet should be enough.
$before_sql = "SELECT ID, post_title, post_excerpt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type = 'post' and post_date < '$cur_post_date' ORDER BY post_date DESC LIMIT $thumbnum";
$before_results = $wpdb->get_results($before_sql);
if($before_results) {
foreach ($before_results as $before_result) {
$post_title = stripslashes($before_result->post_title);
$permalink = get_permalink($before_result->ID);
$post_excerpt = ($before_result->post_excerpt);
$output="<div class=\"thumbnails\">" . $post_excerpt . "<br />‹</div>\n " . $output;
}
}
To get the first attachment with the posts in one query you can do in this way
SELECT *,
(SELECT guid FROM `wp_posts` WHERE post_type ='attachment' AND post_parent=wp.`ID` ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM `wp_posts` wp
ORDER BY post_date ASC will get the first image if you want the latest uploaded image you can simply use DESC ORDER BY post_date DESC
Here is your query
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' ORDER BY wp.post_date DESC LIMIT $thumbnum";
It works fine for me
This is the query which will fetch those posts only which has the attachments on it
$before_sql = "SELECT ID, post_title, post_excerpt,
(SELECT guid FROM $wpdb->posts WHERE post_type ='attachment' AND post_parent=wp.`ID`
ORDER BY post_date ASC LIMIT 1 ) AS attachment
FROM $wpdb->posts wp WHERE wp.post_status = 'publish' AND wp.post_type = 'post'
and wp.post_date < '$cur_post_date' HAVING attachment IS NOT NULL ORDER BY wp.post_date DESC LIMIT $thumbnum";
I'm using wp-ecommerce plugin for my own online ordering bakeshop (http://www.kingsbakery.mpkproducts.com/)
I don't want the way how customers made an order. I want similar order form like this (https://www.ssfamousdeli.com/order_ship.aspx).
I run a custom query in Mysql and I can only retrieve the product title and description. I use this sql statement:
<?php
$sql = "SELECT * FROM wp_posts WHERE
post_type like 'wpsc_product' AND
post_status like 'publish'
ORDER BY ID Asc";
$query = mysql_query($sql);
?>
I only stop there coz I don't know how to join wp_postmeta to select the price.
Please help. Thanks
<?php
$sql = "SELECT wp_p.*, wp_pm.meta_value FROM wp_posts wp_p, wp_postmeta wp_pm WHERE
wp_p.post_type like 'wpsc_product' AND
wp_p.post_status like 'publish' AND
wp_p.ID = wp_pm.post_id
wp_pm.key = 'Price' --This should be the meta key
ORDER BY wp_p.ID Asc";
$query = mysql_query($sql);
?>
Hope this will be of help.