I've been bashing my head against my desk over this for the past day or so...I have created a custom post type called 'advertisement' and a custom taxonomy within that post type called 'group'. I've created a bunch of different posts and assigned them to the various groups that I have created, but the problem is when I try to query the posts for my custom taxonomy, it comes up empty.
When I try to query a regular post with the default category it works fine, so I'm beginning to think it has something to do with the way I have setup my taxonomies. I've already tried resetting permalinks, but to no luck.
Here is what I have so far:
If anyone could shed some light on where I might have gone wrong, that would be wonderful.
Thanks!
/*-----------------------------------------------------------------------------------*/
/* Initialize the post type
/*-----------------------------------------------------------------------------------*/
/**
* Custom Post Type: Advertisements
*
* @package WordPress
* @subpackage ExportWise
*/
/* Register custom taxonomies on the 'init' hook. */
add_action( 'init', 'register_taxonomy_groups' );
function register_taxonomy_groups() {
$labels = array(
'name' => __( 'Groups', 'bd' ),
'singular_name' => __( 'Group', 'bd' ),
'search_items' => __( 'Search Groups', 'bd' ),
'popular_items' => __( 'Popular Groups', 'bd' ),
'all_items' => __( 'All Groups', 'bd' ),
'parent_item' => __( 'Parent Group', 'bd' ),
'parent_item_colon' => __( 'Parent Group:', 'bd' ),
'edit_item' => __( 'Edit Group', 'bd' ),
'update_item' => __( 'Update Group', 'bd' ),
'add_new_item' => __( 'Add New Group', 'bd' ),
'new_item_name' => __( 'New Group', 'bd' ),
'separate_items_with_commas' => __( 'Separate groups with commas', 'bd' ),
'add_or_remove_items' => __( 'Add or remove groups', 'bd' ),
'choose_from_most_used' => __( 'Choose from the most used groups', 'bd' ),
'menu_name' => __( 'Groups', 'bd' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_tagcloud' => false,
'show_admin_column' => true,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
register_taxonomy( 'group', array('advertisement'), $args );
}
/* Register custom post types on the 'init' hook. */
add_action( 'init', 'my_register_post_types', 20 );
/**
* Registers post types.
*
* @since 0.1.0
* @access public
* @return void
*/
function my_register_post_types() {
/* Set up the arguments for the post type. */
$args = array(
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-chart-line',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => true,
'has_archive' => false,
'query_var' => true,
'rewrite' => array(
'with_front' => false,
'pages' => true,
'feeds' => true,
),
'supports' => array( 'title' ),
'taxonomies' => array( 'group' ),
'labels' => array(
'name' => __( 'Advertisements', 'bd' ),
'singular_name' => __( 'Advertisement', 'bd' ),
'menu_name' => __( 'Advertisements', 'bd' ),
'name_admin_bar' => __( 'Advertisements', 'bd' ),
'add_new' => __( 'Add New', 'bd' ),
'add_new_item' => __( 'Add New Advertisement', 'bd' ),
'edit_item' => __( 'Edit Advertisement', 'bd' ),
'new_item' => __( 'New Advertisement', 'bd' ),
'view_item' => __( 'View Advertisement', 'bd' ),
'search_items' => __( 'Search Advertisements', 'bd' ),
'not_found' => __( 'No advertisements found', 'bd' ),
'not_found_in_trash' => __( 'No advertisements found in trash', 'bd' ),
'all_items' => __( 'All Advertisements', 'bd' ),
'parent_item' => __( 'Parent Advertisement', 'bd' ),
'parent_item_colon' => __( 'Parent Advertisement:', 'bd' ),
)
);
/* Register the post type. */
register_post_type(
'advertisement',
$args
);
}
/*-----------------------------------------------------------------------------------*/
/* Add custom taxonomies to the post type
/*-----------------------------------------------------------------------------------*/
function create_my_taxonomies() {
register_taxonomy(
'group',
'advertisement',
array(
'labels' => array(
'name' => 'Groups',
'add_new_item' => 'Add New Group',
'new_item_name' => "New Group"
),
'show_ui' => true,
'show_tagcloud' => false,
'hierarchical' => true,
'public' => true
)
);
}
add_action( 'init', 'create_my_taxonomies', 0 );
/*-----------------------------------------------------------------------------------*/
/* Query the CPT for Posts from a specific custom taxonomy
/*-----------------------------------------------------------------------------------*/
// If on an Article, display random advertisements
if ( is_single() ) {
$posts_per_page = 1;
$orderby = 'rand';
// Otherwise display the latest advertisement
} else {
$posts_per_page = 1;
$orderby = 'date';
}
$args = array(
'post_type' => 'advertisement',
'tax_query' => array(
array(
'taxonomy' => 'group',
'terms' => array( 'homepage' ),
'field' => 'slug',
),
),
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
);
$query_advertisements = new WP_Query( $args );
//echo $query_advertisements->request; ?>
<?php if ( $query_advertisements->have_posts() ) : ?>
<div class="widget widget_ads_320" id="marketing_box">
<div class="ads320">
<div class="ads-content">
<?php
while ( $query_advertisements->have_posts() ) : $query_advertisements->the_post();
$adv_image = getUploadedImageSrc();
$adv_url = get_post_meta( get_the_ID(), 'adv_url', true );
echo '<a href="' . $adv_url . '"><img src="' . $adv_image . '" alt="' . get_the_title() . '" /></a>';
endwhile;
wp_reset_postdata();
?>
</div>
</div>
</div>
<?php else : ?>
<p>Error retrieving advertisements! Please try again later.<br><br></p>
<?php endif; ?>
$wp_query
variable for store the result of the secondary query, to avoid confusion with the global one with the same name. Where do you place your code? It looks like you're storing it in a template file other thenfunctions.php
?