I have a custome field for my product which I am trying to access for sending to third party shipping.This is my code
global $wpdb;
$baseid = $wpdb->get_var("SELECT meta_value FROM wp_postmeta WHERE (meta_key='baseid' AND post_id = '.$ids.' )");
However the AND condition is not working and I am getting an empty value. If I give only post_id = '.$ids.' I am getting a value but not the required value.
Kidnly help!
Just use the appropriate function
http://codex.wordpress.org/Function_Reference/get_post_meta
<?php
$meta_value = get_post_meta( $post_id, 'baseid', true );
?>
Related
i try to show a specific meta_value based on post_id and meta_key in woocommerce order dashboard:
First i create a new column:
function mb_set_order_note_column( $columns ) {
$columns['parcel_delivery'] = __('DHL','TEXTDOMAIN');
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'mb_set_order_note_column', 99 );
After i have tried to get the specific value from wp_postmeta database
function mb_show_order_note_columns( $column_name, $post_id ) {
switch ( $column_name ) {
case 'parcel_delivery':
$order = new WC_Order( $post_id );
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in' );
print $delivery ;
break;
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'mb_show_order_note_columns', 10, 2 );
this shows only the customer notes from wp_post database. how to change to get specific value...
Thx
I see your custom meta key is _parcel_delivery_opted_in. So to get this value from wp_postmeta with order id and meta key,
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in', true ); // like _billing_email meta
3rd parameter true will return single value, if you omit third parameter an array will be returned.
I'm developing wordpress plugin.
I need to find out post_id from thumbnail_id(not reverse !).
How can I do this?
You can get result by this code
global $wpdb;
$_thumbnail_id = {thumbnail id};
$sql = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = $_thumbnail_id";
$result = $wpdb->get_results( $sql, ARRAY_A );
//access first returned post id
var_dump($result[0]['post_id']);
If you added same image for multiple posts there will be multiple returns.
You can use get_the_ID() to get the post id. You can find this function in wp-includes/post-template.php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
Two questions here.
Below is a function in WooCommerce wc-order-functions.php that I found works almost similar to what I need.
function wc_get_order_id_by_order_key( $order_key ) {
global $wpdb;
// Faster than get_posts()
$order_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND meta_value = %s", $order_key ) );
return $order_id;
}
Which returns the $order_id but I'm looking to get the $order_key so that I can contruct the url generated after buyer have click the 'Place order' button on the checkout page e.g. domain.com/checkout/order-received/2316/?key=wc_order_54c7142660e24
wc_order_54c7142660e24 being the $order_id and
2316 being the $order_key
I know that the SQL command below gets the value I want from database:
function wc_get_order_key_by_order_id( $order_id ) {
global $wpdb;
// Faster than get_posts()
$order_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->prefix}postmeta WHERE meta_key = '_order_key' AND post_id = %s", $order_id ) );
return $order_key;
}
But I'm not sure how to hook the filter to wordpress functions.php So this is my first question.
Second question is I've been searching for the function that might be responsible for generating the meta_value for the $order_key but can't seem to find it, would be great to know how it works. I suppose the first way is kinda redundant since the function I'm looking for already exists, just can't seem to locate it.
Try adding this to your woocommerce template files where you need that link:
echo get_site_url().'/checkout/order-received/'.$order->id.'&key='.$order->order_key;
I am trying to query the wordpress post_meta table by meta_value.
I would like to output all post_id's where the meta_value is = to _parent_product. Here is my code:
$posts = $wpdb->get_results("SELECT *, FROM $table WHERE meta_key='_parent_product' ");
foreach ( $posts as $post ){
$id = $post->post_id;
echo $id;
}
The above outputs nothing and im not quite sure why? Can anyone see anything wrong?
As stated in the comment,
There is a comma (,) behind SELECT *,. Therefor the given SQL is invalid and will fail to retrieve any results.
Example, I have a post's meta_key is 'name', meta_value is 'Jason',
and this meta_id in MySQL is 128
How can I get this meta_value by meta_id ?
The best way to do this is to use WordPress's built-in function, get_metadata_by_mid().
So in your case:
$value = get_metadata_by_mid( 'post', 128 );
I found this which might help you.
It describes How to get the Meta ID by Meta Key, so it is not exactly what you need but quite similar. Maybe all that is required is to modify that query a little.
get_metadata_by_mid( $meta_type, $meta_key ) will work for most situations. It returns an object with all the postmeta data.
$jason = get_metadata_by_mid( 'post', 128 );
echo 'Meta Value = ' . $jason->meta_value;
echo 'Post ID = ' . $jason->post_id;
// Results
Meta Value = Jason
Post ID = 123
If you need to do something a bit more custom such as get stock status of product using the meta id. You can use this.
global $wpdb;
$meta_id = 128;
$postmeta_post_id = $wpdb->get_var( $wpdb->prepare("SELECT post_id FROM {$wpdb->prefix}postmeta WHERE meta_id = %d", $meta_id) );
$stock_status = get_post_meta( $postmeta_post_id, '_stock_status', single );
echo 'Product status: ' . $stock_status;
//results
Product status: outofstock