Avoiding “Usage of a direct database call is discouraged”

Using $wpdb->insert and or related methods to modify data within any of the default WordPress tables, be it posts, postmeta, user, usermeta etc is discouraged because there are functions which already exist for the purpose of modififying data within those tables. For example, wp_insert_post wp_update_post wp_delete_post Database Queries Avoid touching the database directly. If there … Read more

How to call a PHP function with Ajax when the user clicks a button

Create a child theme so you don’t mess with the code of an existing theme because next time you’ll update the theme, you may loose all your changes (see Child Themes) Here is how to pass values to your ajax request: jQuery(document).ready(function() { jQuery(“.mark-as-read”).click(function () { console.log(‘The function is hooked up’); jQuery.ajax({ type: “POST”, url: … Read more

wpdb::prepare was called incorrectly

It’s always advised to use $wpdb->prepare when you are taking input from user. This will help in protecting queries against SQL Injection. For more details, check the Codex When you use $wpdb->prepare, you must pass the variables to the query. In your case, you can skip using $wpdb->prepare as you are using a hard coded … Read more

$wpdb->delete column values IN ARRAY()?

No, wpdb::delete does not handle anything other than WHERE field = X. You can just use the query method instead: $ids = implode( ‘,’, array_map( ‘absint’, $ids ) ); $wpdb->query( “DELETE FROM table_name WHERE ID IN($ids)” );

find a random blogid across my multisite network that has at least one post published

An example that displays an admin notice listing a randomized array with all blog IDs, the result of a get_posts( array( ‘numberposts’ => 1 ) ) and marking the first one which get_posts result is different from zero. Result After refreshing: Code add_action( ‘admin_notices’, ‘wpse_60401_print_random_blog’ ); function wpse_60401_print_random_blog() { global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( … Read more

Multisite posts in categories on network

Found the answer, posting the code below so it may help future generations. <?php global $wpdb; $count_terms = $wpdb->get_var (“SELECT COUNT(*) FROM wp_2_term_taxonomy, wp_2_posts, wp_2_term_relationships WHERE wp_2_posts.ID = wp_2_term_relationships.object_id AND wp_2_term_relationships.term_taxonomy_id = wp_2_term_taxonomy.term_taxonomy_id AND wp_2_term_taxonomy.term_id = ’22’ AND wp_2_posts.post_type=”post” AND wp_2_posts.post_status=”publish” “); $count_terms1 = $wpdb->get_var (“SELECT COUNT(*) FROM wp_3_term_taxonomy, wp_3_posts, wp_3_term_relationships WHERE wp_3_posts.ID = wp_3_term_relationships.object_id … Read more

$wpdb and MySQL Create Trigger

In the question you linked to, the solution was to use mysqli_multi_query as the API for executing the SQL. $wpdb desen’t have an API to do a multi query therefor you can’t use it directly, but you can get the handle to the mysql interface from $wpdb->dbh and do something like $sql_trigger = “….”; mysqli_multi_query($wpdb->dbh,$sql_trigger);

wpdb get posts by taxonomy SQL

Sorry guys, I just found the solution: SELECT p.post_name, t.name as clientName FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON (‘p.ID’ = tr.object_id) INNER JOIN $wpdb->term_taxonomy AS tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms AS t ON (t.term_id = tt.term_id) WHERE p.post_status=”publish” AND p.post_type=”portfolio” AND tt.taxonomy = ‘clients’ ORDER BY p.post_date DESC … Read more

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