Export SQL query based on post type

In your WordPress Dashboard, if you go to Tools > Export, you can select your custom post type and export only posts in that post type. It gives you the option between All Content, Posts, Pages, and your custom post types following those three options. As for SQL, well are you looking to export the … Read more

I can’t figure out what’s wrong with this statement. $wpdb->query update

Without an error message, it is difficult to tell the exact problem. It is important to debug within WordPress and check your webserver error logs. One problem is your call of prepare(). The method takes 2 or more arguments, you only passed 1. Instead try the following $update_status = $wpdb->query($wpdb->prepare( “UPDATE {$wpdb->prefix}wcpv_commissions SET commission_status = … Read more

How to create multiple database tables on plugin activation?

Why only the second? Because dbDelta() supports CREATE TABLE table_name format only. I.e. Exactly “CREATE TABLE” followed by (one space and) the table name. More specifically, dbDelta() uses this regular expression pattern: CREATE TABLE ([^ ]*) when parsing the queries into an array indexed by the table name; i.e. array( ‘table_1’ => ‘query’, ‘table_2’ => … Read more

Code to remove authors with no posts connected to them

<?php /** * Plugin Name: Delete Non Authors */ function delete_non_authors() { global $wpdb; $non_authors = $wpdb->get_col( “SELECT DISTINCT $wpdb->users.ID FROM $wpdb->users LEFT JOIN $wpdb->posts ON $wpdb->users.ID = $wpdb->posts.post_author WHERE $wpdb->posts.ID IS NULL” ); foreach ($non_authors as $user_ID) wp_delete_user($user_ID); } register_activation_hook(__FILE__, ‘delete_non_authors’); ?> Drop this in a file, name it something like delete-non-authors.php, upload it … Read more

Get all child comments ids from parent comment id

I found this post because I was looking for a similar solution. As suggested by Captain47, get_comments with parent args, won’t work because it’s return only 1 level nested comments. To get unlimited nested you need to use hierarchical arg as: $args = array( ‘parent’ => $comment_ID, ‘hierarchical’ => true, ); $questions = get_comments($args); Reference: … Read more

WordPress SQL Issue not returning correct reselts

Here is my answer in code form: <?php // Customer Details $args = array( ‘blog_id’ => $GLOBALS[‘blog_id’], ‘role’ => ‘customer’, ‘meta_key’ => ‘last_name’, ‘meta_value’ => ”, ‘meta_compare’ => ”, ‘meta_query’ => array(), ‘date_query’ => array(), ‘include’ => array(), ‘exclude’ => array(), ‘orderby’ => ‘last_name’, ‘order’ => ‘ASC’, ‘offset’ => ”, ‘search’ => ”, ‘number’ => … Read more

Get posts under a category with SQL

SELECT p.ID AS postId, p.post_name, p.post_title, p.post_content, p.post_excerpt, p.post_status, p.post_type, p.post_author, p.guid, p.post_modified_gmt, (SELECT group_concat(t.name SEPARATOR ‘, ‘) FROM wp_terms t LEFT JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id LEFT JOIN wp_term_relationships tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = ‘category’ AND p.ID = tr.object_id ) AS category FROM `wp_posts` AS p LEFT JOIN `wp_term_relationships` … Read more

Custom SQL Query: Get all posts with category id and a concated list of tags on each post

SELECT DISTINCT p.ID AS id, p.post_title AS title, ( SELECT group_concat(p2.guid SEPARATOR ‘, ‘) FROM wp_postmeta pm LEFT JOIN wp_posts p2 ON pm.meta_value = p2.ID WHERE pm.post_id = p.ID AND pm.meta_key = ‘_thumbnail_id’ AND p2.post_type=”attachment” ) AS image, ( SELECT group_concat(pm.meta_value SEPARATOR ‘, ‘) FROM wp_postmeta pm WHERE pm.post_id = p.ID AND pm.meta_key = ‘views’ … Read more

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