0

I'm using this code to first display each category term as headlines and then display a post which is assigned each category term under the headline.

If I create a custom taxonomy it works as intended, but if I attempt to use the default category taxonomy, the headlines display as intended, but under each headline, it displays every article without regard to the assigned term. I don't understand why would it work for my custom taxonomy but not for the default category taxonomy.

<?php $args = array (
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => true,
);

$terms = get_terms( 'category', $args );
foreach ( $terms as $term ) {
    echo $term->name;

    $post_args = array (
        'category' => $term->name,
        'posts_per_page'   => '1',
        'no_found_rows' => true
    );

    $query = new WP_Query( $post_args );

    while ( $query->have_posts() ) {
        $query->the_post(); ?>

        <?php the_title(); ?>
        <?php the_excerpt(); ?>

    <?php }
}

If I change it to use my custom taxonomy named 'article_subjects' in this example, everything works as intended.

<?php $args = array (
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => true,
);

$terms = get_terms( 'article_subjects', $args );
foreach ( $terms as $term ) {
    echo $term->name;

    $post_args = array (
        'article_subjects' => $term->name,
        'posts_per_page'   => '1',
        'no_found_rows' => true
    );

    $query = new WP_Query( $post_args );

    while ( $query->have_posts() ) {
        $query->the_post(); ?>

        <?php the_title(); ?>
        <?php the_excerpt(); ?>

    <?php }
}

And just in case it's pertinant, here is the code I used to generate the custom taxonomy.

function article_subjects() {
$labels = array(
    'name'                       => _x( 'Subjects', 'Taxonomy General Name', 'dc' ),
    'singular_name'              => _x( 'Subject', 'Taxonomy Singular Name', 'dc' ),
    'menu_name'                  => __( 'Subjects', 'dc' ),
    'all_items'                  => __( 'All Items', 'dc' ),
    'parent_item'                => __( 'Parent Item', 'dc' ),
    'parent_item_colon'          => __( 'Parent Item:', 'dc' ),
    'new_item_name'              => __( 'New Subject', 'dc' ),
    'add_new_item'               => __( 'Add New Item', 'dc' ),
    'edit_item'                  => __( 'Edit Item', 'dc' ),
    'update_item'                => __( 'Update Item', 'dc' ),
    'separate_items_with_commas' => __( 'Separate items with commas', 'dc' ),
    'search_items'               => __( 'Search Items', 'dc' ),
    'add_or_remove_items'        => __( 'Add or remove items', 'dc' ),
    'choose_from_most_used'      => __( 'Choose from the most used items', 'dc' ),
    'not_found'                  => __( 'Not Found', 'dc' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => false,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'has_archive'                => true,
    'can_export'                 => true

);
register_taxonomy( 'article_subjects', array( 'post' ), $args );
}
add_action( 'init', 'article_subjects', 0 );
0
2

Your problem lies in this line of code in your first piece of code

'category' => $term->name,

There are two problems here, first of all, category is not a valid parameter for WP_Query, you should use category_name, as you are working with get_terms. get_terms does not return the ID like get_categories, which is an alternative for the first part of code. Secondly, If you use category_name, remember, this is the slug of the category, not the category name

So replace

'category' => $term->name,

with

'category_name' => $term->slug,

As said, you can also use get_categories, so you can do something like this as well

<?php $args = array (
    'orderby'    => 'name',
    'order'      => 'ASC',
    'hide_empty' => true,
);

$categories = get_categories( $args );
foreach ( $categories as $category ) {
    echo $category->name;

    $post_args = array (
        'cat' => $category->cat_ID,
        'posts_per_page'   => '1',
   );

    $query = new WP_Query( $post_args );

    while ( $query->have_posts() ) {
        $query->the_post(); ?>

        <?php the_title(); ?>
        <?php the_excerpt(); ?>

    <?php }
    wp_reset_postdata();
}

?>

Just to point out, you are passing a few invalid arguments to register_taxonomy. Please go and check the codex for valid arguments

EDIT

From your comments, I think you are missing the closing endif; To display the_post_thumbnail, your code will look like this

if ( has_post_thumbnail() ) :
  the_post_thumbnail();
endif;

Just one more thing I forgot to mention yesterday, you should always reset the postdata (wp_reset_postdata)when you use a custom query

3
  • Thanks. Would there be any reason why I can't display the featured_image with this? Since I corrected the code as you suggested, I haven't been able to get the featured images to display. Just using if(has_post_thumbnail()) the_post_thumbnail(); inside while ($query->have_posts()) – davidcondrey Jun 29 '14 at 22:36
  • Please see my edit – Pieter Goosen Jun 30 '14 at 4:32
  • I already took care of it before I saw your reply but thank you very much for taking the time to reply. – davidcondrey Jul 1 '14 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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