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.

Help with Related Posts Function

You can use WordPress’s tax_query arg when fetching posts to achieve what you’re trying to do, this is also easily extendable with more than one taxonomy. $query = array( ‘post_type’ => $YOUR_POST_TYPE, ‘posts_per_page’ => -1 // get all ‘tax_query’ => array( ‘relation’ => ‘OR’ ) ); $query[‘tax_query’][] = array( ‘taxonomy’ => ‘YOUR_TAXONOMY’, ‘terms’ => $ARRAY_WITH_TERMS, … Read more

Select From wpdb – Author/User Directory page

You’re probably better off to use get_users which returns an array of <?php // get all users, regardless of roll. If you do need to restrict by // role you can use the `role` argument: get_users(array(‘role’ => ‘author’)); $uses = get_users(array(‘orderby’ => ‘nicename’)); foreach ($users as $user) { // do stuff with $user, will have … Read more

accessing wordpress serialized data outside wp

Use PHP’s unserialize function: <?php $data=”a:2:{i:0;s:12:”Sample array”;i:1;a:2:{i:0;s:5:”Apple”;i:1;s:6:”Orange”;}}”; $unserialized = unserialize($data); echo ‘<pre>’; print_r($unserialized); echo ‘</pre>’; ?> Result: Array ( [0] => Sample array [1] => Array ( [0] => Apple [1] => Orange ) ) Got the sample data over at unserialize.com, a handy little site if you want to quickly check what’s inside that … Read more

WPDB Query Question with Category Only

This is a SQL problem. Your query is wrong. You forgot to include the term_relationships table in the query, yet reference it during the JOIN. By including the term_relationships table, that would solve the problem with the query. SELECT ID, post_title, post_content, post_date FROM {$wpdb->posts} AS p JOIN {$wpdb->term_relationships} AS tr ON tr.object_id = p.id … Read more

Get all sticky posts from one user through user ID

Copy and paste this function into the function.php file under current theme. // Create a new filtering function that will add where clause to the query function filter_where( $where=”” ) { // posts in the last 30 days $where .= ” AND post_date > ‘” . date(‘Y-m-d’, strtotime(‘-30 days’)) . “‘”; return $where; } Paste … Read more

wpdb prepare sql problem

To get a % character into your SQL string without confusing sprintf() add it to the replacement string: $alabala_sql = $wpdb->prepare( ” SELECT * FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id=tt.term_taxonomy_id INNER JOIN $wpdb->terms t ON t.term_id=tt.term_id and t.slug= %s WHERE p.post_title LIKE %s AND … Read more

Show all 12 months regardless they have posts

I believe this is correct: $year = 2013; $month = 0; $qry = new WP_Query( array( ‘post_type’=>’post’, ‘posts_per_page’=>-1, ‘orderby’=>’date’, ‘order’=>’ASC’, ‘ignore_sticky_posts’ => true, ‘year’ => $year, ) ); while ($month < 12) { $month++; echo date(‘M’,strtotime(‘2000-‘.str_pad($month, 2, ‘0’, STR_PAD_LEFT).’-01 00:00:01′)).'<br>’; if ($qry->have_posts()) { while ($qry->have_posts()) { if (date(‘n’,strtotime($qry->post->post_date)) == $month) { echo ” — “; … Read more

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