WordPress SQL JOIN query

If you are going to do this is SQL, use a subquery. SELECT *, (SELECT meta_value FROM wp_commentmeta WHERE meta_key = ‘your-meta-key’ AND wp_commentmeta.comment_id = wp_comments.comment_ID LIMIT 1) as comment_author FROM wp_comments Instead of the *, enumerate the fields you want but leave out comment_author. Obviously, $wpdb functions to keep the table names straight.

Reordering content using a meta value

Check WP_Query Order by Parameters $args = array( ‘post_type’ => ‘my_custom_post_type’, ‘meta_key’ => ‘age’, ‘orderby’ => ‘meta_value_num’, ‘order’ => ‘ASC’, ‘meta_query’ => array( array( ‘key’ => ‘age’, ‘value’ => array(3, 4), ‘compare’ => ‘IN’, ) ) ); $query = new WP_Query($args);

How to join tables?

There are few things you should remember when you’re using SQL in WordPress. The most important, I guess, would be table prefix. So your query should look something like this: global $wpdb; $sql = “SELECT kategorie.id, ogloszenia.kategoria, klient_id “. “FROM {$wpdb->prefix}category kategorie “. “JOIN {$wpdb->prefix}ogloszenia_kupione ogloszenia ON (ogloszenia.id = kategorie.id)”; $results = $wpdb->get_results( $sql );

What does the $posts_join filter join to?

posts_join is only a part of the full SQL query, the table you’re joining to is referenced earlier in the query. You can see the full query with the posts_request filter. See the documentation for the rest of the query filters.

List author’s posts with SQL

Your table names are wrong. note in your first query the use of $wpdb->posts to reference the posts table. however– this is a very simple query that can be done via the API rather than writing SQL queries from scratch. Use a new WP_Query instance instead.

Inner Join user tables to select users with roles

The Problems You risk SQL injection, with your current setup: $wpdb->users.display_name LIKE ‘$ltr%’ The following could also be a problematic part in your SQL query: AND $wpdb->usermeta.meta_value = %artist% i.e. using = instead of LIKE. You’re also missing the quotes: %\”artist\”%, to exclude e.g. bartist or fartist 😉 But you don’t need to construct this … Read more