duanhan3067 2012-04-18 23:26
浏览 56

返回带有多个元键的wpdb并按meta_value排序

I am trying to use the wordpress wpdb class to return custom posts with one of two values for meta keys and then sort those posts by a third meta value. I can get the posts that I want but I just can't seem to figure out how to sort them the way I would like.

The function I have so far is:

function artists_search_country($country_alt){
    global $wpdb;
    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND ((wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = '$country_alt')
        OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = '$country_alt'))
        AND wposts.post_type = 'artists'
        AND wposts.post_status = 'publish'
    ";
    $myposts = $wpdb->get_results($querystr, OBJECT);
    return $myposts;
}

I would like to do something like:

function artists_search_country($country_alt){
    global $wpdb;
    $querystr = "
        SELECT wposts.*
        FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
        WHERE wposts.ID = wpostmeta.post_id
        AND ((wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = '$country_alt')
        OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = '$country_alt'))
        AND wposts.post_type = 'artists'
        AND wposts.post_status = 'publish'
        ORDER BY (wpostmeta.meta_key = '_artist_artist_last_name' AND wpostmeta.value) ASC
    ";
    $myposts = $wpdb->get_results($querystr, OBJECT);
    return $myposts;
}

Please excuse the poor syntax on the ORDER BY line but I just don't know how to approach this one, everything I try breaks the query. Any help would be greatly appreciated as I don't have that much experience with SQL queries.

  • 写回答

1条回答 默认 最新

  • dongshi9407 2012-05-11 21:01
    关注

    Try this SQL statement:

    SELECT wposts.*, l.meta_value as artist_last_name
    FROM $wpdb->posts wposts
        join $wpdb->postmeta wpostmeta
        on wposts.ID = wpostmeta.post_id
        join $wpdn->postmeta l
        on wposts.ID = l.post_id
        and l.meta_key = '_artist_artist_last_name'
    WHERE
    (( wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = '$country_alt')
    OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = '$country_alt') )
    AND wposts.post_type = 'artists'
    AND wposts.post_status = 'publish'
    ORDER BY l.meta_value ASC
    

    You need to make a second join for the artist's last name.

    Another recommendation is to leverage Wordpress' $wpdb->prepare, like this:

    $querystr = "
    SELECT wposts.*, l.meta_value as artist_last_name
    FROM $wpdb->posts wposts
        join $wpdb->postmeta wpostmeta
        on wposts.ID = wpostmeta.post_id
        join $wpdn->postmeta l
        on wposts.ID = l.post_id
        and l.meta_key = '_artist_artist_last_name'
    WHERE
    (( wpostmeta.meta_key = '_artist_country' AND wpostmeta.meta_value = %s)
    OR (wpostmeta.meta_key = '_artist_country_sub' AND wpostmeta.meta_value = %s) )
    AND wposts.post_type = 'artists'
    AND wposts.post_status = 'publish'
    ORDER BY l.meta_value ASC
    ";
    $myposts = $wpdb->get_results($wpdb->prepare( $querystr, $country_alt, $country_alt ), OBJECT);
    

    Hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作