0

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; ?>
3
  • I would use something else than the $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 then functions.php?
    – birgire
    Commented May 2, 2014 at 19:52
  • @birgire the custom post type and taxonomy is stored in an includes directory and being called by functions.php and the actual query code snippet is being executed from the sidebar.php page.
    – Shane
    Commented May 6, 2014 at 17:13
  • I will check it out and let you know tomorrow ... well maybe sooner, check out my answer ;-)
    – birgire
    Commented May 6, 2014 at 18:13

1 Answer 1

0

It looks like you forgot to set the field parameter to search for slugs:

$args = array(
    'post_type' => 'advertisement',
    'tax_query' => array(
        array(
            'taxonomy' => 'group', 
            'terms' => array( 'homepage' ),
            'field' => 'slug',
        )
    )
);

Notice that the taxonomy query uses the term_id as the default search field.

I would also use another variable than $wp_query to store the result of the secondary query.

I hope this solves your problem ;-)

10
  • When I updated my code to use the field of the taxonomy, it detects no posts/advertisements and says: No Advertisements available! as I have set it in the code. What I am wondering is why it says there are no posts when clearly I have posts listed under that taxonomy term. Does everything look right with the way I have setup my CPT and Taxonomy?
    – Shane
    Commented May 6, 2014 at 19:12
  • Can you rename $wp_query to $q and show the result of echo $q->request;?
    – birgire
    Commented May 6, 2014 at 19:20
  • I've updated my answer, here is the print out. SELECT SQL_CALC_FOUND_ROWS ewwp_posts.ID FROM ewwp_posts JOIN ewwp_icl_translations t ON ewwp_posts.ID = t.element_id AND t.element_type = 'post_advertisement' JOIN ewwp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND 0 = 1 AND ewwp_posts.post_type = 'advertisement' AND (ewwp_posts.post_status = 'publish' OR ewwp_posts.post_author = 2 AND ewwp_posts.post_status = 'private') AND t.language_code='en' GROUP BY ewwp_posts.ID ORDER BY ewwp_posts.post_date DESC LIMIT 0, 1 It doesn't even seem to be picking up my tax_query
    – Shane
    Commented May 6, 2014 at 19:26
  • AND 0 = 1 is the part that's stopping it, coming from the tax query part
    – birgire
    Commented May 6, 2014 at 19:31
  • I just tested your code (a light version) and it works on my install with the default theme, after I added the field slug into the taxonomy query. I added the post type / taxonomy registration parts into the functions.php file and tested the loop in single.php. I created few ads with the homepage term in the group taxonomy. Looks like you're using WPML, does it work without it?
    – birgire
    Commented May 6, 2014 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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