Sorting search results by taxonomy terms

Unfortunately, although WP_Query supports the ‘tax_query’ arg, it does not support ordering based on post terms. So you will need to modify the query SQL, as you are doing now. However, you are constructing the ORDER BY clause incorrectly, and that is why it is ordering by post_date. What you need to do is use … Read more

get_results on large datasets

$wpdb doesn’t suit for fetching huge amount of data from database. Why? In your case: $wpdb->get_results( … ) – fetches all results into your RAM at once. It means if you have 4mb, 10mb, or 50mb of data in db, everything will be stored in memory (what is limited as you know). $wpdb->get_results( …, ARRAY_A … Read more

Optimize WordPress Query that take 5 seconds to execute

I had the exact same issue. The problem is not one that can be fixed without modifying some code that you probably shouldn’t (or perhaps writing a filter or a ‘drop-in’). The issue is the CAST directive in the SQL statement. It CASTS the entire table before it does anything, with the amount of records … Read more

Get Post ID by Content

WordPress has a handy function called post_exists() which allows you to find an existing post by its title, content, and/or date. It returns the post ID on success or 0 otherwise. In your case, you can use $post_id = post_exists( ”, ‘{“member_id”:33}’ ) to find the post you’re looking for. Note: That function doesn’t check … Read more

Use wpdb->prepare for `order by` column name

You can’t use prepare for column names, and you can’t really use it for the sort order either. prepare will always quote the string. You will need to swap in the values yourself. Rather than try to “sanitize” the data, I’d use a white-list approach. $orderby = array( ‘date’ => ‘post_date’, // etc ); $sortorder … Read more

$wpdb->get_results(…) returns empty array despite correct query

Use $wpdb->show_errors( true ) before the query and see what error comes back. My problem (because I’ve experienced the same thing) was that I should use $wpdb->posts instead of wp_posts inside the query. The prefix of the tables can change from WP installation to installation or even in the same installation depending on the time … Read more

Creating 20,000 Posts or Pages using a .csv file?

Import posts from a .csv file with WP-CLI If our import.csv is tab delimited, with two columns: Planet Mars The red planet. Planet Earth Our blue planet. then we can parse it with help of this answer, where we use IFS=$’\t’ instead as tab delimiter: while IFS=$’\t’, read col1 col2; \ do \ echo “$col1|$col2”; … Read more

SQL query to extract only the “current” wp_posts?

Rather than constructing query from scratch, it is easier to see what exactly is WordPress querying when API function is used: get_posts(array( ‘numberposts’ => -1, )); var_dump( $wpdb->last_query ); Gives following SQL: SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_type=”post” AND (wp_posts.post_status=”publish”) ORDER BY wp_posts.post_date DESC

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