1

I am attempting to display the latest x posts from all categories using a custom taxonomy called 'case-studies' in WordPress.

I have managed to output all the category names as heading links I have also outputted some posts from the standard posts taxonomy which are in the correct categories, but I can't get this to display anything from my 'case-studies' taxonomy. When I add 'post_type' => 'case-studies' to $post_args query it does not show any results.

if (have_posts()) :

    $tax = 'case-studies';

    $cat_args = array(
        'orderby' => 'name',
        'order' => 'ASC'
    );

    $categories = get_terms($tax, $cat_args);

    foreach($categories as $category) {

        echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' 
            . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' 
            . '>' . $category->name.'</a></p>';

        $post_args = array(
            'posts_per_page' => 10,
            'category_name' => $category->name
        );

        $posts = get_posts($post_args);

        foreach($posts as $post) { ?>
            <a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a><br/>
        <?php }

        echo '<p><a href="' . get_term_link( $category, $tax ) . '" title="' 
            . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' 
            . '>View all posts in ' . $category->name.'</a></p>';
    }

endif;

2 Answers 2

0

The category parameters does not work with custom taxonomies. You need to use a tax_query instead.

In your current code, replace (which is in any case also wrongly used, category_name takes the slug, not name)

'category_name' => $category->name

with

'post_type' => 'case-studies',
'tax_query' => array(
    array(
        'taxonomy' => $tax
        'terms' => $category->term_id,
        'include_children' => false
    )
),
5
  • Thanks but this is still returning no results. Jul 7, 2015 at 12:45
  • This displays the cat ids after each category title, the categories are working correctly, it is the posts which aren't displaying. Jul 7, 2015 at 12:52
  • Custom post type and taxonomy name is case-studies Jul 7, 2015 at 12:54
  • Not really a great idea to have the same name for both, can lead to inconsistant results. Jul 7, 2015 at 12:55
  • See my updated code, also fixed a bug in it. Sorry about that Jul 7, 2015 at 12:59
0

This code display all posts of all categories of location taxonomy for custom post type testaismgallery.

 $args = array(
        'number'     => $number,
        'hide_empty' => $hide_empty,
        'include'    => $ids
    );

    $custom_categories = get_terms( 'location', $args );

    foreach ( $custom_categories as $catterm){

        $arg = Array( 
        'post_type' => 'testaismgallery',
        'posts_per_page' => '-1',
        'post_status' => 'publish',
        'tax_query' => Array( Array ( 
        'taxonomy' => 'location' ,
        'terms' => $catterm->term_id
        )) );


    $loop = new WP_Query( $arg ); 
    global $post;

    ?>    

    <?php    
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
      <div class="gallery-content">
    <div class="entry-content">


    <?php 
    $post_id = get_the_ID();
    $abc = get_post_meta($post_id, event_file, true);
     echo "<li><img src='".$abc. "' ></li> "; 

     ?>  

        </div>
    </div>
     <?php endwhile;

    }   

    ?>
5
  • Can you put this into my code as it hasn't output correctly here. Use phpfiddle.org If I replace my $post_args with your $args and amend the names it displays nothing. Unsure what this code actually does. You need to tab your code four times to display as code. Jul 7, 2015 at 12:24
  • Here in get_terms( 'location', $args ); function you have to provide your taxonomy and tax_query' => Array( Array ( 'taxonomy' => 'location' , 'terms' => $catterm->term_id )) and here we pass term_id of category using tax_query in WP_Query.After that inside while loop you can get display your custom metabox using their meta key.I don't know why echo statement not display code ?
    – Kd dev
    Jul 7, 2015 at 12:31
  • Please clean up your code. Tip, highlight your code while in the editor and click the {} sign. This will add your code in a code block. More so, please add a proper explanation of what your code does and how it works. Code dumping is considered bad answers Jul 7, 2015 at 12:33
  • Okay I am already doing the get_terms correctly in my code above, I am just using a variable instead for where you have 'location' I have added this to my original post_args but it still displays nothing: 'tax_query' => array( array( 'taxonomy' => 'case-studies', 'terms' => $category->term_id Jul 7, 2015 at 12:39
  • You have to give custom post type name inside $post_args . Like ,'post_type' => 'Custom post type'.
    – Kd dev
    Jul 7, 2015 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.