Programmatically changing role information; editing role name and deleting

There’s a whole host of functions specifically for this purpose; http://codex.wordpress.org/Function_Reference#User_and_Author_Functions Of particular interest (but not limited to) are, add_cap add_role get_role map_meta_cap remove_cap remove_role As well as numerous other user related functions that will allow you to verify/validate their authority based upon your use-case scenario and so on. Looking in wp-includes/capabilities.php you we can … Read more

How To Make Connection To WordPress Data Base In A Plugin?

I found the answer my self. First open your wp-config.php and check the bottom of file that Is that contain the below code?… if ( !defined(‘ABSPATH’) ) define(‘ABSPATH’, dirname(__FILE__) . “https://wordpress.stackexchange.com/”); If yes then add the below code to make the connection in your plugin PHP files to connect with wp-config.php file that contain Database … Read more

Custom SQL Query for WordPress page

The wpdb object can be used to run arbitrary queries against the WordPress database. Let’s say you want to list the most recent 4 posts: $results = $wpdb->get_results( “SELECT * FROM $wpdb->posts WHERE `post_type`=’post’ LIMIT 4” ); The $wpdb->posts variable will output the table name for posts. It’s usually wp_posts, but if you’re using a … Read more

Is it useful to turn on MySQL query cache for performance

General speaking yes for read-only applications (until MySQL 5.7, it’s deprecated since version 8.0), it enables equal SELECTs to return data extremely fast if identical calls are already stored in cache. You should consider that: Only exact same clauses can benefit from the cache engine (no spaces, no comments, no actual differences in WHERE expressions); … Read more

Custom SQL query to get List of posts with featured image url

Worked on similar problem recently. Here is the SQL query to get the post with Featured Image. global $wpdb; $perpage = 10; $page = 1; // Get the current page FROM $wp_query $counter = $perpage * $page; $uploadDir = wp_upload_dir(); $uploadDir = $uploadDir[‘baseurl’]; $sql = ” SELECT post.ID, post.post_title, post.post_date, post.category_name, post.category_slug, post.category_id, CONCAT( ‘”.$uploadDir.”‘,”https://wordpress.stackexchange.com/”, … Read more

Manually delete post from database

Drop this into a file in your plugin directory and you should be able to do this from your WP installation using a query string. /* Plugin Name: Delete Specific Post Description: Rid the post forever! Version: 0.1 Author: WPSE License: GPL2 */ add_filter(‘query_vars’, ‘delete_post_query_var’); function delete_post_query_var($vars){ $vars[] = ‘delete_post’; return $vars; } add_action(‘admin_init’, ‘manually_delete_post’, … Read more

Convert WordPress posts to products in WooCommerce

You could install the Post Type Switcher plugin Or Run SQL query on your database to change the post to product post type UPDATE `wp_posts` SET `post_type` = ‘product’ WHERE `post_type` = ‘post’; Backup your DB 1st. UPDATE `wp_posts` SET `post_type` = ‘wpsc-product’ WHERE `post_type` = ‘post’;

Alternative to mysql_real_escape_string

When working with database in WordPress you should never use the low lever mysql_* or mysqli_* functions. Always use $wpdb methods, in your case you should use prepare(): $query = $wpdb->prepare( “SELECT post_title from $wpdb->posts WHERE post_title LIKE %s”, “%” . $myTitle . “%” ); Moreover, once you are getting a single column, you have … Read more