Wordpress:通过自定义选择查询显示帖子

时间:2012-01-09 23:05:28

标签: wordpress

我需要按自定义类别ID获取帖子,按自定义字段值排序,如果此字段存在,则需要使用其他自定义字段。我想我需要使用自定义选择查询。看看我的查询:问题是这个查询返回相同的帖子5次...在管理员面板中,我每页发了20个帖子。也许有人有自己的解决方案?感谢。

$wp_query->request = "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->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($wp_query->request, OBJECT);

3 个答案:

答案 0 :(得分:2)

$customSelect = new WP_Query(array(

    'post_type'       => 'post',      
    'posts_per_page'  => 10,
    'taxonomy'        => 'category',
    'order'           => 'DESC',
    'meta_key'        => 'your custom field',
    'orderby'         => 'meta_value'

这可能与你想要达到的目标有关,我唯一不能理解的是如何使用变量或其他东西测试元键是否存在。

无论如何祝你好运

答案 1 :(得分:0)

为什么你使用“$ wp_query-> request”,这个变量用于检索最后一个查询结果,而不是设置新的...

试试

$my_custom_sql = "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->terms.term_id = $wpdb->term_taxonomy.term_id)
WHERE $wpdb->terms.slug = 'categoryname'
AND $wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
ORDER BY $wpdb->posts.post_date DESC";
$pageposts = $wpdb->get_results($my_custom_sql, OBJECT);

答案 2 :(得分:-1)

  1. 试试这个 -

    get_results( “ SELECT * FROM $ wpdb->帖子 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) 在哪里$ wpdb-> posts.post_status ='发布' AND $ wpdb-> term_taxonomy.taxonomy ='category' AND $ wpdb-> term_taxonomy.term_id = 3 ORDER BY post_date ASC “ ); ?>