开发者

Wordpress SQL Select Multiple Meta Values / Meta Keys / Custom Fields

开发者 https://www.devze.com 2022-12-24 04:22 出处:网络
I am trying to modify a wordpress / MySQL function to display a little more information. I\'m currently running the following query that selects the post, joins the \'postmeta\' and gets the info wher

I am trying to modify a wordpress / MySQL function to display a little more information. I'm currently running the following query that selects the post, joins the 'postmeta' and gets the info where the meta_key = _liked

    function most_liked_posts($numberOf, $before, $after, $show_count) {
 global $wpdb;

    $request = "SELECT ID, post_title, meta_value FROM $wpdb->posts, $wpdb->postmeta";
    $request .= " WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";
    $request .= " AND post_status='publish' AND post_type='post' AND meta_key='_liked' ";
    $request .= " ORDER BY $wpdb->postmeta.meta_value+0 DESC LIMIT $numberOf";
    $posts = $wpdb->get_results($request);

    foreach ($posts as $post) {
     $post_title = stripslashes($post->post_title);
     $permalink = get_permalink($post->ID);
     $post_count = $post->meta_value;

     echo $before.'<a href="' . $permalink . '" title="' . $post_title.'" rel="nofollow">' . $post_title . '</a>';
  echo $show_count == '1' ? ' ('.$post_count.')' : '';
  echo $after;
    }
}

The important part being: $post_count = $post->meta_value;

But now I want to also grab a value that is attached to each post called wbphoto

How do I sp开发者_高级运维ecify that $post_count = _liked and $photo = wbphoto

?

Here is a screen cap of my Phpmyadmin

Wordpress SQL Select Multiple Meta Values / Meta Keys / Custom Fields


The SQL will look very ugly.

// Your Meta key names
    $metas = array(
        '_liked', '_another1'
    );

    foreach ($metas as $i=>$meta_key) {
        $meta_fields[] = 'm' . $i . '.meta_value as ' . $meta_key;
        $meta_joins[] = ' left join ' . $wpdb->postmeta . ' as m' . $i . ' on m' . $i . '.post_id=' . $wpdb->posts . '.ID and m' . $i . '.meta_key="' . $meta_key . '"';
    }
    $request = "SELECT ID, post_title, " .  join(',', $meta_fields) . " FROM $wpdb->posts ";
    $request .=  join(' ', $meta_joins);
    $request .= " WHERE post_status='publish' AND post_type='post'";
    $request .= " LIMIT $numberOf";

It will be better of making another SQL to retrieve meta themselves.


Don't know if that could help you, but here's a working SQl query that get posts and post_meta.

SELECT DISTINCT ID, p.post_title, p.post_status, p.post_date, m.meta_key, m.meta_value 
FROM wp_posts p, wp_postmeta m
WHERE p.post_author = 2 
AND p.post_status = 'publish' 
AND p.post_date >= '2009-01-01' 
AND p.post_date <= '2010-05-28'
AND p.ID = m.post_id


Modify query as below:

$request = "SELECT ID, post_title, meta_value FROM $wpdb->posts p, $wpdb->postmeta pm, $wpdb->postmeta pm1";
    $request .= " WHERE p.ID = pm.post_id";
    $request .= " AND p.post_status='publish' AND p.post_type='post' AND pm.meta_key='_liked' AND pm1.meta_key='wbphoto';
    $request .= " ORDER BY pm.meta_value+0 DESC LIMIT $numberOf";
0

精彩评论

暂无评论...
验证码 换一张
取 消