简体   繁体   中英

List all posts in custom post type by taxonomy not working

I am trying to get a list of all the posts in custom post type by taxonomy, I am stuck 3 days at this code, now i study with my father and he gave me a hint why my code is'nt working he a said i have too many args i will show you the code i hope anyone can help me understand why its not working and maybe if you really kind a explanation of the code in english

print_r(Array(
    "1"=>"first",
    "2"=>"second"
    ));
// just try to remove args that you don't need 
//actually you need only one
$args = array(
    'tax_query' => array(
            'taxonomy' => 'your-custom-taxonomy',
            'field' => 'slug',
            'terms' => array( 'your-term' )
    ),
    'post_type' => 'your-post-type'
);
$loop = new WP_Query($args);
     if($loop->have_posts()) {
    $term = $wp_query->queried_object;
     while($loop->have_posts()) : $loop->the_post();
        //Output what you want      
   echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
}
  1. So you have a custom post type called your-post-type
  2. and you have a custom taxonomy called your-custom-taxonomy and you
  3. want to get all the posts with a taxonomy term set called your-term .

You are doing it the right way with setting your arguments.

Notice : If you want to get all the posts of a custom post type, you do not need the whole 'tax_query' part of code.

I added some comments to describe what the code is doing:

$args = array( // define your arguments for query
    'post_type' => 'your-post-type', // standard post type is 'post', you use a custom one
    'tax_query' => array( // you check for taxonomy field values
        array(
            'taxonomy' => 'your-custom-taxonomy', // standard is 'category' you use a custom one
            'field'    => 'slug', // you want to get the terms by its slug (could also use id)
            'terms'    => 'your-term', // this is the taxonomy term slug the post has set
        ),
    ),
);
$loop = new WP_Query( $args ); // get post objects

// The Loop
if ( $loop ->have_posts() ) { // check if you received post objects
    echo "<ul>"; // open unordered list
    while ( $loop ->have_posts() ) { // loop through post objects
        $loop ->the_post();
            echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>'; // list items
    }
    echo "</ul>"; // close unordered list
    /* Restore original Post Data */
    wp_reset_postdata(); // reset to avoid conflicts
} else {
    // no posts found
}

Hope this helps!


EDIT: If you do not know how to use WP_Query

This code will get your wordpress posts ordered by their titles and output title and content. Put this inside a template file of your theme (learn something about template files: https://developer.wordpress.org/themes/basics/template-hierarchy/ ).

<?php 

$args = array(
 'post_type' => 'post',
 'posts_per_page' => -1, // limit the number of posts if you like to
 'orderby' => 'title',
 'order' => 'ASC'
);

$custom_query = new WP_Query($args); 

if ($custom_query->have_posts()) : while($custom_query->have_posts()) : $custom_query->the_post(); ?> 
  <h1><?php the_title(); ?></h1> 
  <?php the_content();?>
<?php endwhile; else : ?> 
  <p>No posts</p>
<?php endif; wp_reset_postdata(); ?>

You said you want to use a custom post type and do a taxonomy query. So you can adjust this with changing the arguments in $args .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM