自定义帖子类型通过自己的分类法进行过滤

发布于 2025-01-20 18:46:15 字数 1607 浏览 0 评论 0原文

我尝试了很多,但没有成功。这个循环工作得很好。它显示所有自定义帖子类型(应用程序和工具):

<?php
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

现在我想按自定义分类法进行过滤。首先,我使用 get_queried_object() 获取自定义分类法,并将 tax_query 放入循环中的数组中。最后是一些控制输出。

<?php
// getting custom taxonomy of actual post
$this_term = get_queried_object();
$term_id =  $this_term->term_id;
$term_name =  $this_term->name;
$term_taxonomy = $this_term->taxonomy;
$term_slug = $this_term->slug;

// Wordpress Loop
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
          'tax_query' => array(
               array (
                    'taxonomy' => $term_taxonomy,
                    'name' => $term_name
          )
          )
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

//Control Output
echo 'term_id: ' . $this_term->term_id;
echo '<br>';
echo 'name: ' . $this_term->name;
echo '<br>';
echo 'taxonomy: ' . $this_term->taxonomy;
echo '<br>';
echo 'slug: ' . $this_term->slug;
?>

结果是,LOOP 没有显示任何结果。未显示 CPT,但控制输出符合预期。我只想通过自定义分类法过滤我的 CPT。

非常感谢任何帮助,非常感谢。

I tried a lot, but with no success. This LOOP works fine. It shows all Custom Post Types (apps-und-tools):

<?php
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

Now I want to filter by a custom taxonomy. First I get the custom taxonomy with get_queried_object() and put the tax_query to the array in the LOOP. At the end are some control outputs.

<?php
// getting custom taxonomy of actual post
$this_term = get_queried_object();
$term_id =  $this_term->term_id;
$term_name =  $this_term->name;
$term_taxonomy = $this_term->taxonomy;
$term_slug = $this_term->slug;

// Wordpress Loop
$loop = new WP_Query(
     array(
          'post_type' => 'apps-und-tools',
          'posts_per_page' => -1,
          'tax_query' => array(
               array (
                    'taxonomy' => $term_taxonomy,
                    'name' => $term_name
          )
          )
     )
);
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div>
    <h2><?php the_title(); ?></h2>
</div>
<?php endwhile;
wp_reset_postdata();

//Control Output
echo 'term_id: ' . $this_term->term_id;
echo '<br>';
echo 'name: ' . $this_term->name;
echo '<br>';
echo 'taxonomy: ' . $this_term->taxonomy;
echo '<br>';
echo 'slug: ' . $this_term->slug;
?>

The result is, that the LOOP does not show any results. No CPTs are shown, but the control output is as expected. I'd just like to filter my CPTs by custom taxonomy.

Any help is greatly appreciated, many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2025-01-27 18:46:15

请检查此循环以检索CPTS。

$args = array(
 'post_type' => arra('apps-und-tools'),
'tax_query' => array(
    array(
        'taxonomy' => $term_taxonomy,
        'field'    => 'slug',
        'terms'    => $term_slug,
    ),
 ),
);
$query = new WP_Query( $args );

Please check this loop to retrieve CPTs.

$args = array(
 'post_type' => arra('apps-und-tools'),
'tax_query' => array(
    array(
        'taxonomy' => $term_taxonomy,
        'field'    => 'slug',
        'terms'    => $term_slug,
    ),
 ),
);
$query = new WP_Query( $args );
ま昔日黯然 2025-01-27 18:46:15
$term_taxonomy = 'taxonomy_name';
$args = array(
  'post_type' => arra('apps-und-tools'),
  'post_status' => "publish",
  'tax_query' => array(
    array(
      'taxonomy' => $term_taxonomy,
      'field'    => 'slug',
      'terms'    => $search_key,
      ),
    ),
  );
$query = new WP_Query( $args );
$term_taxonomy = 'taxonomy_name';
$args = array(
  'post_type' => arra('apps-und-tools'),
  'post_status' => "publish",
  'tax_query' => array(
    array(
      'taxonomy' => $term_taxonomy,
      'field'    => 'slug',
      'terms'    => $search_key,
      ),
    ),
  );
$query = new WP_Query( $args );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文