Display SQL that wordpress is running

You can use any one of these filters from /wp-includes/query.php posts_where_request posts_groupby_request posts_join_request posts_orderby_request posts_distinct_request posts_fields_request post_limits_request You can print certain parts of the query to the screen to see what’s going on under the hood. For instance, you can add this to your functions file: add_filter(‘posts_where_request’, ‘foo_bar’, 1, 10); function foo_bar($where){ print_r($where); return $where; … Read more

Moving online website to MAMP or dumb idea?

There are some awesome resources online to help you with this process. Good for you for setting up a local development environment. I think you will find it to be an awesome change to your workflow, but be prepared for some trial and error in getting it right. First of all, there is an excellent … Read more

CRUD operations using WordPress database API

Querying in WordPress is done by using the the wpdb variable. Be sure to always prepare your sql so WP can make it database safe. Example: global $wpdb; $rows = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM `{$wpdb->prefix}TABLE_NAME` WHERE `var` = %d”, 1) ); Then you can loop through the data and output it as for example … Read more

Distinction on meta value on pre_get_posts

DISCLAIMER : Not a WordPress Expert, Just an old MySQL DBA Here is your original generated SQL SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish” OR wp_posts.post_status=”private”) AND ( ( wp_postmeta.meta_key = ‘_position’ AND CAST(wp_postmeta.meta_value AS CHAR) > ‘0’ ) ) GROUP BY wp_posts.ID, wp_postmeta.meta_value … Read more

Fetch all Posts where logged in user has commented

It should be simple since comments are stored with a user_id if they are created by a logged in user and if not then its set to false so all you need to do is select the post id from comments that are made by user_id bigger the zero, something like this: function get_posts_with_loogedin_user_comments(){ global … Read more

posts_where Fails with More than One Custom Field in Query

Think about your query. You are asking that $wpdb->postmeta.meta_key be both “foo” and “bar”. That will never happen. If you ran this in a context where you’d get to read MySQL debugging data (PhpMyAdmin’s SQL console will do this) you would probably see a warning about an “impossible where clause”. You need to JOIN on … Read more