bes*_*rld 1 wordpress loops wp-query
我想在下面优化我的代码.地点和舞蹈风格都是类别课程,但是由城镇位置1和城镇位置2(不是位置的是舞蹈风格,反之亦然)标识.
$locations = get_terms("categorycourses", array('include' => array(townLocation1, townLocation2)));
$dance_styles = get_terms("categorycourses", array('exclude' => array(townLocation1, townLocation2)));
$content = '';
foreach ( $locations as $location ) {
$content .= . '<h1>'.$location->name.'</h1>';
foreach ( $dance_styles as $dance_style ) {
//Show courses that is associated with this dance-style
//and in this location
$loop = get_loop_courses($dance_style, $location->term_id);
if($loop->have_posts()) {
//show courses here (post with custom post type course)
}
} //end dance_styles
} //end locations
function get_loop_courses($categorycourse, $location_id) {
$terms_slug = strtolower($categorycourse->slug);
$terms_slug = str_replace(' ','', $terms_slug);
$args = array(
'posts_per_page' => -1, //Show ALL coures (no limit)
'post_type' => 'course',
'tax_query' => array(
array(
'taxonomy' => 'categorycourses',
'field' => 'slug',
'terms' => $terms_slug
),
),
);
$args['tax_query'][] = array(
'taxonomy' => 'categorycourses',
'field' => 'id',
'terms' => array($location_id)
);
}
$loop = new WP_Query($args);
return $loop;
}
Run Code Online (Sandbox Code Playgroud)
在get_loop_courses被调用很多次,因此,当数据库的增长它采取了很多(unnessecary)的时间来加载.我的方法是考虑构建一个参数数组(来自get_loop_courses-function),然后执行WP_Query ONCE并以可管理的方式循环结果集. 这就是我尝试过的:
$locations = get_terms("categorycourses", array('include' => array(townLocation1, townLocation2)));
$dance_styles = get_terms("categorycourses", array('exclude' => array(townLocation1, townLocation2)));
$loop_arr = array();
$content = '';
foreach ( $locations as $location ) {
$content .= . '<h1>'.$location->name.'</h1>';
foreach ( $dance_styles as $dance_style ) {
//Show courses that is associated with this dance-style
//and in this location
$loop_arr[] = get_loop_courses($dance_style, $location->term_id);
} //end dance_styles
} //end locations
$loop = new WP_Query($loop_arr); //this is giving me an array but with a lot of posts, and not the ones I intended
function get_loop_courses($categorycourse, $location_id) {
$terms_slug = strtolower($categorycourse->slug);
$terms_slug = str_replace(' ','', $terms_slug);
$args = array(
'posts_per_page' => -1, //Show ALL coures (no limit)
'post_type' => 'course',
'no_found_rows' => true, //no pagination!
'tax_query' => array(
array(
'taxonomy' => 'categorycourses',
'field' => 'slug',
'terms' => $terms_slug
),
),
);
$args['tax_query'][] = array(
'taxonomy' => 'categorycourses',
'field' => 'id',
'terms' => array($location_id)
);
}
return $args;
}
Run Code Online (Sandbox Code Playgroud)
我在思考"正确"的方向吗?请指点我!这种方法甚至可能无法实现?
在这种情况下是否可以使用瞬态?
如果我们尝试计划数据库增长,我们可能会想出一个像这样的显示模式:

哪里:
看起来您当前的解决方案仅适用于少数帖子和术语(黄色)
有很多帖子和几个术语,我们想要帖子分页(橙色).
由于条款很多且帖子很少,我们希望使用术语分页(绿色).
对于很多帖子和术语,我们都想要两种分页(蓝色)
对于可扩展的分类搜索解决方案,我们应该寻找一种方法WP_Query(),每个搜索查询只运行一次.
看起来你将自定义categorycourses分类法分成两个相互排斥的部分:位置和舞蹈风格.
但我建议使用两种自定义分类法:locations和dancestyle.
这听起来更合乎逻辑.
当您收到大量帖子时,您可以使用本机分类搜索,以显示与给定位置和舞蹈风格相对应的所有帖子.
example.com/?location=london&dancestyle=tango
Run Code Online (Sandbox Code Playgroud)
这将使用主查询,不需要辅助查询.在二十二的主题将使用archive.php模板来显示结果,你会得到分页免费!
如果我们想要使用自定义模板文件而不是archive.php,我们可以使用template_include过滤器对其进行修改.
有很多方法可以为分类搜索创建用户界面.
您可以考虑使用分面搜索UI.检查这个伟大的插件,以获得好的想法:
http://wordpress.org/plugins/query-multiple-taxonomies/screenshots/
这是一个类似的插件列表:
https://github.com/scribu/wp-query-multiple-taxonomies/wiki/Similar-plugins
对于大量术语(数千),我们可以考虑术语分页或某种向下钻取解决方案,如AZ目录列表.
我们可以get_terms()使用以下方法设置术语分页:
get_terms( 'mytaxonomy',
array(
'offset' => $offset,
'number' => $number,
)
);
Run Code Online (Sandbox Code Playgroud)
我希望这对你有所帮助.