Get stock by custom meta field on all Woocommerce variable products

The following function will give you an array with the calculated stock quantity from all product variations for each custom meta field “Brand” value, using WPDB Class (a SQL query): global $wpdb; $results = $wpdb->get_results( ” SELECT pm.meta_value as brand, SUM(pm2.meta_value) as stock FROM {$wpdb->prefix}postmeta pm INNER JOIN {$wpdb->prefix}posts p ON pm.post_id = p.post_parent INNER … Read more

When to call add_filter

Yes, you can do this like that. You can call foo_init_setup on init or on wp hook and it will work just fine. I would call it as late as possible, I guess. This way you won’t affect loading time for requests that don’t end with page rendering (for example when template_redirect is used for … Read more

Show MySQL errors that occur when I excute $wpdb->insert()

This doesn’t answer your question directly, but the solution here is to validate the data before you chuck it at the database and pray. Sending bad data ‘down the line’ so that something else can check it and report back to you if it wasn’t what they were expecting is bad design. You don’t know … Read more

Syntax for $wpdb->prepare when searching in two columns

The part after WHERE decides which columns are searched. So you need to change it this way: $foods = $wpdb->get_results( $wpdb->prepare( “SELECT id, foodname, namevariations, calories, carbs, fat, protein, sodium FROM foodsTable WHERE namevariations like %1$s OR foodname like %1$s”, ‘%’ . $wpdb->esc_like( $search_text ) . ‘%’ ), ARRAY_A ); Also you were using LIKE … Read more

Why is variable not working on custom sql query using wpdb?

Huge thanks to @kero !! I was finally able to create a solution by making the sql statement a variable first (as kero suggested) then using that variable on wpdb->query(); Here’s my updated and fully working codes now: $imdbid = $_GET[‘id’]; $string = file_get_contents(“http://www.omdbapi.com/?i=”.$imdbid.”&apikey=9a187152″); $json = json_decode($string, true); if( $json[‘Response’] === “True” ){ $dataID = … Read more

WPDB If primary key already exists, add +1 to integer field

This ended up being the correct formula in my case: $sql = “INSERT INTO foodWishes(foodWishName,foodWishPoints) VALUES (%s,%d) ON DUPLICATE KEY UPDATE foodWishPoints = foodWishPoints +1”; // var_dump($sql); // debug $sql = $wpdb->prepare($sql,$sanitized_foodWish,$foodWishPoint,); // var_dump($sql); // debug $wpdb->query($sql); if($sql){ //return the response echo $sql; }else{ echo “something went very very wrong”; } exit();

wpdb get_results() and prepare when to use prepare?

so if i have a function that gets terms from the database ( not the user ) do I need to use prepare first ( before get_results() ), or some sort of data sanitizing? Yes, but you should be using get_terms/WP_Term_Query/wp_get_object_terms/etc and the other term APIs instead as they’re safer and can be much faster. … Read more

In what part of the WordPress core does the users table and usermeta table get joined?

I’m not 100% what you are asking, it seems like several questions? But here goes: $meta_value = get_user_meta($user_id, $key, $single); For example: $first_name = get_user_meta($user_id, ‘first_name’, true); As for adding hooks I think this answer might be what you are looking for? How To Add Custom Form Fields To The User Profile Page? UPDATE Based … Read more

$wpdb->flush(); breaks the loop

In the code you posted in your other question, your call to flush() is using the wrong variable: $wpdp->flush();. Since this is an undefined variable, it’s likely that you’re simply getting a PHP runtime error at this point, stopping page execution.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)