How do I get one cell value from $wpdb? - wordpress

I can't figure out what function should I use, get_var, get_results or may be I shouldn't use them at all, but I neet to echo just one value from one cell only, like 9000.
<?php $apartprice_this = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM wp_postmeta WHERE post_id=%d AND meta_key=%s", the_ID(),'price'));
echo $apartprice_this; ?>
but now, I don't know where it takes, but it returns me "8Array", what is that?

WP function get_results returns an array.
To echo any value in that array you should use the field key as:
foreach( $apartprice_this as $results )
{
echo $results->field_key;
}

Related

Accessing database in wordpress

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 );
?>

wordpress database select security

I'm returning some data from custom table, but I'm not sure if is the code enough secured. Can you help me?
$page_title = get_the_title();
$id = $wpdb->get_row('select id from table where name="'.htmlspecialchars($page_title).'"');
Not sure why you would match a title, usually when doing a search its better to use an id.
But this is what a custom query would look like to get a post id when the title matches the var. prepare does the proper escaping. See the first place prepare comes up here http://codex.wordpress.org/Class_Reference/wpdb
global $wpdb;
$page_title = get_the_title();
$id = $wpdb->get_row($wpdb->prepare('SELECT id FROM ' . $wpdb->posts . ' WHERE post_title = %s', $post_title));
Or you could use WordPress's esc_sql() function on your var.
http://codex.wordpress.org/Function_Reference/esc_sql
But like I said, this query seems backwards. If you have get_the_title() you should also have the_ID() or get_the_ID() so the query is not needed. So it depends what you are trying to do.

Fetch custom field data for a specific blog post

I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What's the proper way to output the content of the custom field?
The easiest way to do this is this:
<?php echo get_post_meta($post->ID, 'your_meta_key', true); ?>
On your post or page editor, you can go to "Screen Options" in the top right corner and check the box to display "Custom Fields". This will allow you to see the meta keys available. Just copy the name of the meta key into your get_post_meta call in the spot above where it says "your_meta_key". Don't change the $post->ID as that is global.
Taken from that page linked
<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
so you'd need to access it through the $meta_values return object.
Like so:
<?php
print_r($meta_values);
print 'The ID is: ' . $meta_values->ID;
?>
get_post_meta(1, 'Currently Reading',true); will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:
<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a print or echo like so:
echo $custom;
Something to note, try using a value $post_id for the first argument. This will grab the current post id.

wordpress get post_id where meta_value =

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.

Can WordPress WP_Query return custom field post meta?

For some reason, I cannot get WP_Query to return the custom field values of posts. I can get the post thumbnails using get_the_post_thumbnail($post->ID, array(50,50)), but I cannot get the custom field data using get_post_meta($post->ID, $key, true).
A stripped-down version of what I'm trying to do:
<?php
$keys = array('Show Date','Birth Year','Origin');
echo '<table>';
echo '<tr><th>Title</th>';
foreach( $keys as $key ) {
echo '<th>' . $key . '<th>';
}
echo '</tr>';
$myquery = new WP_Query( 'post_type=post' );
if( $myquery->have_posts() ) : while( $myquery->have_posts() ) : $myquery->the_post();
$title = get_the_title();
echo '<tr><td>' . $title . '</td>';
$values = array();
foreach( $keys as $key ) {
$values[] = get_post_meta($post->ID, $key, true);
}
foreach( $values as $value ) {
echo '<td>';
echo $value;
echo '</td>';
}
echo '</tr>';
endwhile; endif;
echo '</table>';
?>
Also available here:
http://pastebin.com/at8S2THs
Even with all non-essential code removed, I cannot make this work. I know you can use parameters like meta_key and meta_value in a query to narrow down the results, but I just want to display all values for the keys I specify, if they exist, for each post.
Any help would be greatly appreciated...
** SOLUTION FOUND **
Just needed to add global $post; after the start of the loop. Thanks to #Kimikaze on the WP support forum for providing the solution!
When I can't find the data I need in wordpress, I always find it helpful to print the Global $post object to screen, so I can see if my data is making it to the page.
Global $post;
echo "<pre>";
print_r($post);
echo "</pre>";
All the helper methods or "hooks" are usually just interacting with this (or another) global object. Check the output of this on $post (and maybe your $values array) for data you're looking for and if it's there, you can just reffer to it directly, like the post title for example
$post->title
Another thought, The third parameter of get_post_meta() makes it return a single string when set to true, what do you get when it's set to false?
I'm guessing the problem has to do with the values you're using as meta keys. To retrieve the data with get_post_meta() you'll want to pass in the actual meta_key values, which are probably 'show_date', 'birth_year', and 'origin' since the meta_key value doesn't handle uppercase letters or spaces. Try changing to those values where you're setting the $keys array:
$keys = array('show_date','birth_year','origin');
If that doesn't work, it might be worth checking in the database (in the wp_postmeta table) to confirm the actual meta_key values.

Resources