我正在执行以下操作,以获取与某个帖子相关的所有孩子:
$args = array(
\'order\'=> \'ASC\',
\'post_parent\' => get_the_ID(),
\'post_status\' => \'publish\',
\'post_type\' => \'task\'
);
$tasks = get_children( $args );
现在类型的帖子
task
将Posteta与之关联。一个特定的meta\\u键是
task_progress
. 我想让所有的孩子
task_progress
meta\\u值小于100。过滤结果的最佳方法是什么?
最合适的回答,由SO网友:EarnestoDev 整理而成
这应该可以做到:
global $wpdb;
$parent = 1; // Make sure it\'s int (intval() it) as it\'s not escaped in the query
$posts = $wpdb->get_results(
"SELECT `post_id` AS `ID`, `meta_value` AS `Progress` FROM {$wpdb->postmeta}
WHERE `meta_key`=\'task_progress\' AND `meta_value` < 100 AND `ID` IN
(SELECT `post_id` FROM {$wpdb->posts} WHERE `post_parent`={$parent})
ORDER BY `meta_value` DESC, `post_id` DESC;" // Sort descending
);
foreach($posts as $post){
$progress = $post->Progress;
$post = get_post($ID = $post->ID);
// Print here
}
问候您。