• Resolved segan93

    (@segan93)


    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!

    /*-----------------------------------------------------------------------------------*/
        /*	Located in /includes/custom-post-types/
        /*-----------------------------------------------------------------------------------*/
    
        // Initialize the post type
    
        function cpt_advertisements() {
        	$labels = array(
        		'name'					=> __( 'Advertisements', 'post type general name' ),
        		'singular_name'			=> __( 'Advertisement', 'post type singular name' ),
        		'add_new'				=> __( 'Add New', 'advertisement' ),
        		'add_new_item'			=> __( 'Add New Advertisement' ),
        		'edit_item'				=> __( 'Edit Advertisement' ),
        		'new_item'				=> __( 'New Advertisement' ),
        		'all_items'				=> __( 'All Advertisements' ),
        		'view_item'				=> __( 'View Advertisements' ),
        		'search_items'			=> __( 'Search Advertisements' ),
        		'not_found'				=> __( 'No advertisements found' ),
        		'not_found_in_trash'	=> __( 'No advertisements found in the Trash' ),
        		'parent_item_colon'		=> '',
        		'menu_name'				=> 'Advertisements',
        	);
        	$args = array(
        		'labels'				=> $labels,
        		'description'			=> 'Holds our advertisements and advertisement specific data',
        		'public'				=> true,
        		'menu_position'			=> 20,
        		'supports'				=> array( 'title' ),
        		'has_archive'			=> false,
        		'menu_icon'				=> 'dashicons-chart-line',
        		'taxonomies'			=> array( 'group' ),
        	);
        	register_post_type( 'advertisement', $args );
        }
        add_action( 'init', 'cpt_advertisements' );
    
        // 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 );
    
        /*-----------------------------------------------------------------------------------*/
        /*	Located in: sidebar.php
        /*-----------------------------------------------------------------------------------*/
    		// Query the CPT for Posts from a specific custom taxonomy
    		$args = array(
    			'post_type' => 'advertisement',
    			'tax_query' => array(
    				array(
    					'taxonomy' => 'group',
    					'terms' => array( 'homepage' ),
    				)
    			)
    		);
    
    		$wp_query = new WP_Query( $args );
    
    	?>
    
    	<?php if ( $wp_query->have_posts() ) : ?>
    		<div class="widget" id="advertisement_widget">
    
    			while ( $wp_query->have_posts() ) : $wp_query->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>
    	<?php else : ?>
    		<p>No Advertisements available!</p>
    	<?php endif; ?>
Viewing 1 replies (of 1 total)
  • Thread Starter segan93

    (@segan93)

    This is now resolved. The problem was with the way the custom post type was being included in the functions.php file.

Viewing 1 replies (of 1 total)
  • The topic ‘Query Posts with Custom Taxonomy from a Custom Post Type’ is closed to new replies.