针对分类术语和后期元数据的Wordpress自定义查询

时间:2013-09-04 20:07:28

标签: php mysql wordpress

我有这个我需要做的自定义查询,它将检查post_metadata值和分类术语。我使用的是从Wordpress的codex修改过的代码,但它一致地返回零结果。我做错了吗? (更新:显示首先将其加载到变量中的位置)

global $wpdb;

$querystr = "
    SELECT * FROM $wpdb->posts
    LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->terms.name = '" . $service . "'
    AND $wpdb->term_taxonomy.taxonomy = 'Services'
    AND $wpdb->postmeta.meta_value = '" . $county . "'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'post'
    AND $wpdb->postmeta.meta_key = 'order'
    ORDER BY $wpdb->postmeta.meta_value ASC";

$pageposts = $wpdb->get_results($querystr, OBJECT);

1 个答案:

答案 0 :(得分:0)

我建议记录执行的实际字符串(将此select保存到变量并记录变量,然后将变量传递给wpdb查询)。获得sql后,您可以更轻松地对其进行故障排除(或在此处发布以获得进一步的帮助)。