stephen
stephen

Reputation: 51

Wordpress Loop for Custom Post Type specific sub-category only

I've been trying to display all of the posts inside a specific sub category within my custom post type called 'Training'. The sub-category is called 'spotlight-on-events' & i have been trying to display each post which is placed inside the category but im getting none found. I have tried numerous different things & checked the functions and there's nothing I can see which should be affecting this. Could any WordPress experts take a loop & see what might the problem be please.

Here's the query:

  <?php

$date_args_side = array(
    'category_name' => 'spotlight-on-events',
    'post_type' => 'training',
    'posts_per_page' => 10,

);

$date_query_side = new WP_Query( $date_args_side );

?>  

<aside>
  <div class="small-12 large-3 large-offset-1 columns webinar-event" style="padding:0;overflow:hidden;">
    <h2>Spotlight On Events</h2>
    <hr>
    <?php if( $date_query_side->have_posts()  ): ?>
    <?php while( $date_query_side->have_posts() ) : $date_query_side->the_post(); ?>
    <div class="full-width">
      <div class="small-12 large-12 columns" style="padding:0">
        <?php 
    $attachment_id = get_post_thumbnail_id(); // attachment ID
    $image_attributes = wp_get_attachment_image_src( $attachment_id,'full' ); // returns an array
  ?>
        <img src="<?php bloginfo('template_directory'); ?>/imageResizer/imgresize.php?src=<?php echo $image_attributes[0]; ?>&h=500&w=750&q=95"> </div>
      <div class="small-12 large-12 columns white upcoming-events-side"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" class="event-date">
        <?php the_title(); ?>
        </a> <span class="alignright date-updates">
        <?php $dateformatstring = "l d F"; $unixtimestamp = strtotime(get_field('date_picker'));echo date_i18n($dateformatstring, $unixtimestamp); ?>
        </span>
        <p><?php echo get_excerpt(180); ?></p>
        <a href="<?php the_field('webinar_url'); ?>" class="call2actionbuttonred">Read More</a> </div>
    </div>
    <?php endwhile; ?>
    <?php else: ?>
    <?php _e('There are no upcoming Spotlight Events','example'); ?>
    <?php endif; wp_reset_query(); ?>
  </div>
</aside>

Here is the functions.php setup for the custom post type too:

    function training_taxonomy() {
 register_taxonomy(
 'training', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
 'training', //post type name
 array(
 'hierarchical' => true,
 'label' => 'Training Categories', //Display name
 'query_var' => true
 )
 );
}

add_action( 'init', 'register_themepost2', 20 );

function register_themepost2() {

 $labels = array(
 'name' => _x( 'Training', 'catchthemes_custom_post','catchthemes' ),
 'singular_name' => _x( 'Training', 'catchthemes_custom_post', 'catchthemes' ),
 'add_new' => _x( 'Add New', 'catchthemes_custom_post', 'catchthemes' ),
 'add_new_item' => _x( 'Add New Training', 'catchthemes_custom_post', 'catchthemes' ),
 'edit_item' => _x( 'Edit Training', 'catchthemes_custom_post', 'catchthemes' ),
 'new_item' => _x( 'New Training', 'catchthemes_custom_post', 'catchthemes' ),
 'view_item' => _x( 'View Training', 'catchthemes_custom_post', 'catchthemes' ),
 'search_items' => _x( 'Search Training', 'catchthemes_custom_post', 'catchthemes' ),
 'not_found' => _x( 'No Training found', 'catchthemes_custom_post', 'catchthemes' ),
 'not_found_in_trash' => _x( 'No Training found in Trash', 'catchthemes_custom_post', 'catchthemes' ),
 'parent_item_colon' => _x( 'Parent Training:', 'catchthemes_custom_post', 'catchthemes' ),
 'menu_name' => _x( 'Webinars & Events', 'catchthemes_custom_post', 'catchthemes' ),

 );

 $args = array(
 'labels' => $labels,
 'hierarchical' => true,
 'description' => 'Training',
 'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
 'show_ui' => true,
 'show_in_menu' => true,
 'menu_position' => 5,
 'show_in_nav_menus' => true,
 'publicly_queryable' => true,
 'exclude_from_search' => false,
 'query_var' => true,
 'can_export' => true,
 'public' => true,
 'rewrite' => array('slug' => 'training','with_front' => false),
 'has_archive' => true,
 'capability_type' => 'post'
 );

 register_post_type( 'training', $args );//max 20 charachter cannot contain capital letters and spaces

}

add_action( 'init', 'training_taxonomy');

As you can see from the query I have stripped it right down just to get it working but its

Thank you

Upvotes: 1

Views: 756

Answers (1)

stephen
stephen

Reputation: 51

for anyone else wondering how to do this, I changed the above query to this:

  <?php
$date_args_side = array(
    'post_type'   => 'training',
    'tax_query' => array(
        array(
            'taxonomy' => 'training',
            'field' => 'slug',
            'terms' =>   'spotlight-on-events',
        )
     ),
    'posts_per_page' => 10,

);
$date_query_side = new WP_Query( $date_args_side );
?>

This now works a treat - thanks Pieter Goosen

Upvotes: 1

Related Questions