List posts that have the current url taxonomy

admin2025-06-06  2

I created a custom post type "projects" and then I created a taxonomy to categorize my projects "categoriesprojects".

I have dedicated a page that lists my projects randomly via WP_Query (archive-projets.php) in this way:

<?php 
    $projectslist = array(
      'post_type'=>'projets',
      'orderby' => 'rand'
    ); 
?> 
<?php $loop = new WP_Query($projectslist); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

So, if I launch mywebsite/projects, my projects are all listed, but now I would like to list projects related to a category only (taxonomy "categoriesprojects"), this way if I launch mywebsite/projects for example, I would like to have only posts in the "events" category.

I created a custom post type "projects" and then I created a taxonomy to categorize my projects "categoriesprojects".

I have dedicated a page that lists my projects randomly via WP_Query (archive-projets.php) in this way:

<?php 
    $projectslist = array(
      'post_type'=>'projets',
      'orderby' => 'rand'
    ); 
?> 
<?php $loop = new WP_Query($projectslist); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

So, if I launch mywebsite/projects, my projects are all listed, but now I would like to list projects related to a category only (taxonomy "categoriesprojects"), this way if I launch mywebsite/projects for example, I would like to have only posts in the "events" category.

Share Improve this question edited Nov 6, 2018 at 12:45 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Nov 6, 2018 at 1:45 FloriaanFloriaan 1 0
Add a comment  | 

1 Answer 1

Reset to default 0

You should try like

<?php 
    $projectslist = array(
      'post_type'=>'projets',
      'orderby' => 'rand',
      'tax_query' => array(
        array(
            'taxonomy' => 'categoriesprojects',
            'field'    => 'slug',
            'terms'    => 'events',
        ),
      ),
    ); 
?> 

Hope it will help you.

Please let me know if any query.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749200666a317207.html

最新回复(0)