exclude pingbacks from wordpress SQL query

You can achieve this by excluding pingback from the comment_type column of wordpress comments table $query = “select wp_posts.*, coalesce(( select max(comment_date) from $wpdb->comments wpc where wpc.comment_post_id = wp_posts.id AND comment_approved = 1 AND post_password = ” AND comment_type NOT IN ( ‘pingback’ ) ), wp_posts.post_date ) as mcomment_date from $wpdb->posts wp_posts where post_type=”post” and … Read more

Waypoints + infinite scroll with custom query

You never seem to populate the $paged variable with a value. Try adding this before the query (from the codex): if ( get_query_var( ‘paged’ ) ) { $paged = get_query_var( ‘paged’ ); } elseif ( get_query_var( ‘page’ ) ) { $paged = get_query_var( ‘page’ ); } else { $paged = 1; } Also, your “load … Read more

WordPress SQL search, how to handle SQL Injection?

The default public search query variable is s but not q. So when you use q, on a vanilla WP install, no posts filtering is done, as expected. You don’t have to take care of possible SQL injections, for the default search query, as it’s taken care of by the core. Here’s an example when … Read more

$wpdb->prepare affecting the query?

You can use the array directly because the $wpdb->prepare() method can be called like vsprintf(), which works like sprintf() but accepts arrays of arguments. $post_types = get_post_types(); unset( $post_types[‘attachment’], $post_types[‘nav_menu_item’] ); global $wpdb; $replace_query = $wpdb->get_results( $wpdb->prepare( “SELECT * FROM $wpdb->posts WHERE post_type IN ( %s )”, $post_types ), ARRAY_A ); The problem with your … Read more

Query posts based on previous query

As discussed in the comments, instead of using the ID query argument to get each ‘calp_event’-type post individually, you can pass an array of IDs to post__in in order to grab them all at once, thus reducing the number of database transactions in your code to 2. By creating an array mapping post IDs to … Read more

Query Nickname rather than Display Name in custom Woocommerce plugin

I had a similar problem and solved it similar to this: add_filter(‘pre_user_display_name’,’default_display_name’); function default_display_name($display_name) { if ( isset( $_POST[‘billing_first_name’] ) ) { $display_name = sanitize_text_field( $_POST[‘billing_first_name’] ); } return $name; } I think, the new woocommerce changes the disyplay_name on every checkout to First_Name Last_Name. With the ‘pre_user_display_name’ you can change your own way.

How to write update query in WordPress to expire transients

Transients are not guaranteed to use database at all. They will use object cache if it is enabled. And since there is no bulk delete in object cache API, effectively there is no reliable way to clear out transients across all possible environments. There are some performance reasons to clear them out of database (which … Read more

Random Page Rewrite

use this template in a single page <?php /* Template Name: Random3 */ ?> <?php $args = array( ‘numberposts’ => 1, ‘orderby’ => ‘rand’, ‘post_type’ => ‘food’ ); // get a random post from the database $my_random_post = get_posts ( $args ); // process the database request through a foreach loop foreach ( $my_random_post as … Read more

Query the WordPress database to get data together with replaced information

This query worked for me. I had to reverse it, starting at the user and then matching the post ID. You would need to replace the value of wp_posts.ID of course. SELECT wp_users.ID FROM wp_users LEFT JOIN wp_postmeta ON wp_postmeta.meta_value = wp_users.user_email AND wp_postmeta.meta_key = ‘_from_email’ LEFT JOIN wp_posts ON wp_posts.ID = wp_postmeta.post_id AND wp_posts.ID … Read more

Parse query filter in wordpress and relation

Since you are querying for multiple meta_key’s, you will want to put your meta_key queries in an array. A good example is this posting on StackExchange. For each of your functions you’ll want to replace the last three lines where you set the [‘meta_key’], [‘meta_value’], and [‘meta_relation’] with something like this: if( ! isset($query->query_vars[‘meta_query’]) ) … Read more

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