Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query posts under custom taxonomy WordPress

  • SOLVED

Hi have a custom post type called events, under the event I have a custom taxonomy called coursetype

eg:



add_action( 'init', 'create_event' );
function create_event() {
$labels = array(
'name' => _x('Event', 'post type general name'),
'singular_name' => _x('Event', 'post type singular name'),
'add_new' => _x('Add New Event', 'Project'),
'add_new_item' => __('Add Event'),
'edit_item' => __('Edit Event'),
'new_item' => __('New Event'),
'view_item' => __('View Event'),
'search_items' => __('Search Event'),
'not_found' => __('No Event found'),
'not_found_in_trash' => __('No Event found in Trash'),
'parent_item_colon' => ''
);

$supports = array('title');

register_taxonomy(
'Types',
'event',
array(
'label' => __( 'Course type' ),
'rewrite' => array( 'slug' => 'coursetype' ),
'hierarchical' => true,
)
);

register_post_type( 'event',
array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'supports' => $supports,
'rewrite' => array('slug' => 'whats-on'),
'query_var' => true,
'menu_icon' => 'dashicons-calendar-alt'
)
);

}



I am trying to display posts under the 'adult' custom taxonomy, here is where I have got to, but this does not seem to work.



<?php
global $post;
$tmp_post = $post;
$args = array(
'post_type' => 'event',
'numberposts' => 99,
'tax_query' => array(
array(
'taxonomy' => 'coursetype',
'field' => 'slug',
'terms' => 'adult'
),
)
);
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>

<p><?php the_title(); ?></p>

<?php endforeach; ?>
<?php $post = $tmp_post;
wp_reset_query();
?>






Answers (2)

2014-09-24

John Cotton answers:


register_taxonomy(

'Types',


should be

register_taxonomy(

'coursetype',


Nick comments:

Doah, thanks John! That's did the trick


John Cotton comments:

Sometimes, it just needs someone else to look :)

2014-09-24

Martin Pham answers:


register_taxonomy(
'Types',...


To

register_taxonomy(
'adult',
.....



Martin Pham comments:

I changed position "register_"


add_action( 'init', 'create_event' );
function create_event() {
$labels = array(
'name' => _x('Event', 'post type general name'),
'singular_name' => _x('Event', 'post type singular name'),
'add_new' => _x('Add New Event', 'Project'),
'add_new_item' => __('Add Event'),
'edit_item' => __('Edit Event'),
'new_item' => __('New Event'),
'view_item' => __('View Event'),
'search_items' => __('Search Event'),
'not_found' => __('No Event found'),
'not_found_in_trash' => __('No Event found in Trash'),
'parent_item_colon' => ''
);

$supports = array('title');

register_post_type( 'event',
array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'supports' => $supports,
'rewrite' => array('slug' => 'whats-on'),
'query_var' => true,
'menu_icon' => 'dashicons-calendar-alt'
)
);

register_taxonomy(
'adult',
'event',
array(
'label' => __( 'Course type' ),
'rewrite' => array( 'slug' => 'coursetype' ),
'hierarchical' => true,
)
);

}