wordpress get post_id where meta_value = - wordpress

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.

Related

Wordpress - Insert last update date in header

I added a line in my Wordpress HTML code ( header.php ) to set the date of last update. You understand that i have to make it everytime i make a new post or put a new product. Is there a way to do it automatically? I'd like to get the date of the last post/product and echo it in the header. My knowledge of PHP is poor, sorry for that.
Add this code in functions.php
<?php
function last_updated(){
global $wpdb;
$sql = "SELECT post_modified
FROM $wpdb->posts
WHERE post_type='post' OR post_type='product'
AND post_status='publish'
ORDER BY post_modified DESC
LIMIT 1";
$last_update = $wpdb->get_var( $sql );
$last_update = date("d-m-Y", strtotime($last_update));
return $last_update;
}
add_shortcode('last_updated_date','last_updated');
?>
Add this code in header.php
<?php echo do_shortcode('[last_updated_date]');?>

Get all Custom posts beginning with letter A

I have a custom post type(event) with a custom taxonomies(event_countries).
http://educationgate.org/en/event_countries/united-arab-emirates/
I want to get all the custom posts that bening with the letter A.
The issue is i want to get this through a url string.
I have tried http://educationgate.org/en/event_countries/united-arab-emirates?orderby=title&order=ASC&s=A
But this is going to the search page and show all posts.
Can somebody help?
This must be helpful
global $wpdb;
$request = "A"
$results = $wpdb->get_results(
"
SELECT * FROM $wpdb->posts
WHERE post_title LIKE '$request%'
AND post_type = 'event'
AND post_status = 'publish';
"
);

Add Custom Function to WooCommerce

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;

How do I get one cell value from $wpdb?

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;
}

wordpress num rows, how?

I'm creating a plugin for wordpress and I need to check if a post with a specific title exist or not. This to prevent duplicates, how can I query this and see if it returned true or false?
I’m using this code to get the ID of a post/page by title:
function get_id_by_name($name)
{
global $wpdb;
$id = $wpdb->get_col(
"SELECT ID FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_title = '$name'
LIMIT 1");
return empty ( $id ) ? -1: (int) $id[0];
}
If the post doesn’t exists, the function return -1.

Resources