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
Related
Im trying to make what seems like a simple adjustment to a plugin im using, unfortunately im just a 'wannabe programmer' and not the real deal so i wa shoping someone whom is a real programmer can help me out here. If you can help me i would be foreever grateful and even willing to pay a bit for someone to explain this process to me...... here goes:
Im working with a plugin, it has a list of posts recently created and shows ALL posts no matter which user i login with... I would like to change this to show ONLY post by the user whom is logged in... below is the file\
<?php
global $wpdb;
$current_time = current_time('mysql') + 60;
$ereminder_array = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM {$wpdb->posts}
WHERE post_date <= %s
AND post_type ='ereminder'
AND post_status = 'draft'
AND author = 'wp_current_user'
ORDER BY post_date ASC
", $current_time) );
$scheduled_data = array(
'list' => $ereminder_array,
'type' => 'scheduled'
);
echo PDER_Utils::get_view( 'ereminder-list.php', $scheduled_data );
?>
if anybody can tell me how to alter above code to show only post by the user that is logged in instead of all the users like it does now i would be forever grateful.
thanks in advance
Try this,
<?php
$current_user = wp_get_current_user();
global $wpdb;
$current_time = current_time('mysql') + 60;
$ereminder_array = $wpdb->get_results( $wpdb->prepare("
SELECT *
FROM $wpdb->posts
WHERE post_date <= %s
AND post_type ='ereminder'
AND post_status = 'publish'
AND post_author = %d
ORDER BY post_date ASC
", $current_time, $current_user->ID) );
$scheduled_data = array(
'list' => $ereminder_array,
'type' => 'scheduled'
);
echo PDER_Utils::get_view( 'ereminder-list.php', $scheduled_data );
?>
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 );
?>
We are able to make use of ajax to update our post_meta as we wanted. However, it does not change the modified_time of the post.
We depend on the get_modified_time to show users when the post was last updated. (The newer the better)
I have searched around, and I don't see anyone using this technique yet.
Does anyone have an answer?
Thanks!
I used wpdb::query() to do this:
global $wpdb;
//eg. time one year ago..
$time = time() - DAY_IN_SECONDS * 365;
$mysql_time_format= "Y-m-d H:i:s";
$post_modified = gmdate( $mysql_time_format, $time );
$post_modified_gmt = gmdate( $mysql_time_format, ( $time + get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
$post_id = /*the post id*/;
$wpdb->query("UPDATE $wpdb->posts SET post_modified = '{$post_modified}', post_modified_gmt = '{$post_modified_gmt}' WHERE ID = {$post_id}" );
Note: You can't use wp_update_post() if you want to explicity set the modified date(s) on the post, because it calls wp_insert_post(), which determines that the post exists and sets the post_modified and post_modified variables to the current date.
Very simple in PHP, where 80 is the post number:
// update post_modified and post_modified_gmt `datetime` on a post
$update = array( 'ID' => 80 );
wp_update_post( $update );
If you want to change for set of posts, better use Query Loop to get each post id.
Assuming that you have previously collected the post ID from your ajax request you can then update post_modified and post_modified_gmt using the regular wp_update_post function:
$date = current_time('mysql');
wp_update_post(array(
'ID' => $post_id,
'post_modified' => $date,
'post_modified_gmt' => get_gmt_from_date($date),
));
Resources
https://developer.wordpress.org/reference/functions/wp_update_post/
Currently i have 2 categories and 1 custom field
Category = CAT01 and CAT02 / ID's 1 and 2
Custom Field = Test01
What I want is the average of all values entered in my custom field (Test01), but from 1 category.
I'm new to Wordpress, but I got the following:
$wp_query = new WP_Query( array (
'category_id' => '1',
'meta_key' => 'Test01',
)
);
Without the category id it seems to do something, but not what I want.
Can anyone help me out with this, really appreciate the effort :)
I think using WP_Query adds needless overhead to this. I would do a custom SQL query (although you'd have to keep an eye on if WP changes their taxonomy DB structure in the future).
It would look something like this:
global $wpdb;
$term = get_term(1, 'category'); //need the term_taxonomy_id, not always the same as term_id
$sqlQuery = "SELECT AVG(meta_value) FROM $wpdb->postmeta WHERE meta_key = 'Test01' AND post_id IN(SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = '" . $term->term_taxonomy_id . "') ";
$avg = $wpdb->get_var($sqlQuery);
Wordpress 3.0
I want to have the contents of a specific post into a page by using the title of the post. As far as I can tell, I can't do it directly with get_post().
I can assume what the brute force way might be, but I suspect there's a more elegant way?
get_page_by_title($id, OBJECT, 'post');
There ye go.
<!--1.Get post ID by post title if you know the title or the title variable-->
<?php
$posttitle = 'post_title';
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
echo $postid;
?>
<!--2.use get_post($post_id) to get whatever you want to echo-->
<?php
$getpost= get_post($postid);
$postcontent= $getpost->post_content;
echo $postcontent;
?>
No need to SQL query's when you can use wordpress own functions for this.
$page = get_page_by_title( 'Startsida' );
$page_id = $page->ID;
post_exists is a good function for that :
https://developer.wordpress.org/reference/functions/post_exists/
<?php
$post_id = post_exists('Your title');
// return id or 0 if post doesn't exists.
if($post_id>0)
get_post($post_id);
See my answer on a very similar question. Do not query the data base with an unescaped string.
You can use this:
1)
global $wpdb;
$your_title = "yourtitle";
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = $your_title");
echo $id;
or 2)
$slug_to_get = 'my_title_or_slug';
// you can use custom post type too
$posttypee='post';
$args=array(
'title' => $slug_to_get,
'post_type' => $posttypee,
'post_status' => 'publish'
);
$my_posts = get_posts($args);
if( $my_posts ) {
echo 'ID on the first post found '.$my_posts[0]->ID;
}