好的,我需要使用以下条件查询帖子:
category\\u name=office,meta\\u key=featured\\u post,meta\\u value=Yes
并使用第二个具有数值的自定义字段对这些结果进行排序
meta\\u键=prop\\u顺序
我有以下查询,它提取了正确的帖子,但没有按第二个自定义字段对它们进行排序。
<?php $recent = new WP_Query("category_name=office&meta_key=featured_post&meta_value=Yes&posts_per_page=3&orderby=date&order=ASC"); while($recent->have_posts()) : $recent->the_post(); ?>
但是,由于我已经在查询中使用了自定义字段,所以无法使用orderby=meta\\u值。
我在论坛上找到了以下SQL查询,但无法使其工作。
<?php
global $wpdb;
global $post;
$querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS proporder ON(
$wpdb->posts.ID = proporder.post_id
AND proporder.meta_key = \'prop_order\'
)
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)
WHERE $wpdb->term_taxonomy.term_id = 3
AND $wpdb->term_taxonomy.taxonomy = \'category\'
AND $wpdb->posts.post_status = \'publish\'
AND $wpdb->wpostmeta.meta_key = \'featured_post\'
AND $wpdb->wpostmeta.meta_value = \'Yes\'
ORDER BY proporder.meta_value ASC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
?>
任何帮助都将不胜感激!
谢谢
戴夫
最合适的回答,由SO网友:daveaspinall 整理而成
谢谢大家的意见。我没意识到我从来没有回复过这个!感谢Ethan和其他几位foums,以下是工作代码:
<?php
global $wpdb;
global $post;
$querystr = "
SELECT * FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta AS proporder ON(
$wpdb->posts.ID = proporder.post_id
AND proporder.meta_key = \'prop_order\'
)
LEFT JOIN $wpdb->postmeta AS propfeatured ON(
$wpdb->posts.ID = propfeatured.post_id
AND propfeatured.meta_key = \'featured_post\'
)
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)
WHERE $wpdb->term_taxonomy.term_id = 4
AND $wpdb->term_taxonomy.taxonomy = \'category\'
AND $wpdb->posts.post_status = \'publish\'
AND propfeatured.meta_value=\'Yes\'
ORDER BY proporder.meta_value ASC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);
//print_r($querystr);
?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
<!-- some post stuff here -->
<?php endforeach; ?>
其中,“prop\\u order”和“characterized\\u post”是自定义字段,返回与characterized\\u post=\'Yes\'匹配的帖子,然后按ASC顺序中的“prop\\u order”进行排序。
希望这有帮助!