Query to sort a list by meta key first (if it exists), and show remaining posts without meta key ordered by title

I’m working on a custom taxonomy term page template where we want the items that are connected to the term sorted by a publication date (custom date field) – and if there are multiple items on the same day (formatted like YYYY-MM-DD) to then sort those by title, and finally sort by title if the … Read more

$wpdb->get_row() only returns a single row?

Why is it? I tried the same query in the console and it returned multiple rows. Here’s the query: $this->wpdb->get_row(“SELECT * FROM “.$this->wpdb->users.” WHERE status=”active””, ARRAY_A); It keeps returning the same single row when there are several active users. Am I missing something? 4 Indeed, use get_row() only when you expect to get one result, … Read more

How to print the excuted sql right after its execution

I am searching for a way by which i can print the executed sql query just after the : $wpdb->query( $wpdb->prepare(“INSERT INTO tbl_watchprosite SET keywords=%s,url_to_post=%s,description=%s, date_captured=%s,crawl_id=%d, image_main=%s,images=%s,brand=%s, series=%s,model=%s,condition=%s,box=%s, papers=%s,year=%s,case_size=%s,status=%s,listed=%s, asking_price=%s,retail_price=%s,payment_info=%s,forum_id=%d”, $this->getForumSettings()->search_meta,$element->href,$post_meta[‘description’],current_time(‘mysql’),$cid,$post_meta[‘image_main’],$images,$post_meta[0],$post_meta[1],$post_meta[2],$post_meta[3],$post_meta[4],$post_meta[5],$post_meta[6],$post_meta[7],$status,$post_meta[9],$post_meta[10],$post_meta[11],$this->getForumSettings()->ID) ); This would be great if i can see what values are going in the query. Thanks 4 The $wpdb object has some properties … Read more

How do you properly prepare a %LIKE% SQL statement?

I’d like to use a LIKE %text% statement while still using the WordPress $wpdb class to sanitize and prepare input. SELECT column_1 from `prefix_my_table` WHERE column_2 LIKE ‘%something%’; I’ve tried something like this to no avail: $wpdb->prepare( “SELECT column_1 from `{$wpdb->base_prefix}my_table` WHERE column_2 LIKE %s;”, like_escape($number_to_put_in_like)); How do you properly prepare a %LIKE% SQL statement … Read more