使用两种分类法对自定义帖子类型进行排序


Sorting custom post type using two taxononmies

我有麻烦弄清楚如何排序我的自定义帖子类型,有2个分类法。

分类1)位置(即;多伦多,纽约)-鼻涕虫:位置分类2)贸易(即:木匠,电工)- slug: trade

在查看多伦多页面时(使用我的分类法模板:taxonomy-location.php),我希望能够对多伦多列出的所有交易进行排序。它应该看起来像这样:

,

Toronto (Page title)

木匠

木匠1名,木匠2名,木匠3名,木匠4名

电工

电工1姓名,电工2姓名,电工3姓名,电工4姓名

,

我在我的自定义分类法模板(taxonomy-location.php)中创建查询循环,因此我假设显示的帖子已经在多伦多位置标记,但情况似乎并非如此。

下面是我的taxonomy-location.php文件中的循环:
<?php
$args=array(
‘trade’ => ‘carpenters’,
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

如果我在第一个数组中添加另一个用于位置分类的参数,它可以工作-但我不希望它仅限于toronto。我希望它从任何位置页面你正在观看(即。纽约,多伦多等)。

<?php
$args=array(
‘trade’ => ‘carpenters’,
'location' => ‘toronto’, // this should be something like this <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->name; ?>
'post_type' => 'post_type_name',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'Carpenters';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

实现这一点的最好方法是什么?

您可以使用get_query_var作为位置,但是您必须首先注册它以便Wordpress访问。您必须为所有自定义变量执行此操作:

function add_query_vars_filter( $vars ){
  $vars[] = "location";
  return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );