I am checking the current page I am on and then using that to set a variable to change the category name like so.

        if (is_page('apples')) {
            $categoryname = 'Apples';

        $args = array ( 
            'posts_per_page' => -1,
            'post_type' => 'products',
            'category_name' => $categoryname

        $fruit_query = new WP_Query( $args );

        if ( $fruit_query->have_posts() ):
            while ( $fruit_query->have_posts() ) :

                if ( has_post_thumbnail() ) { 

                <strong><?php the_title(); ?></strong><br />
                Harvest Week: From <?php the_field('harvest_week_from'); ?> to <?php the_field('harvest_week_to'); ?><br />
                Colour: <?php the_field('colour'); ?><br />
                Market Season: <?php the_field('market_season'); ?><br />
                Market Sector: <?php the_field('market_sector'); ?><br />
                <a href="<?php the_permalink();?>">Link to product</a><br />

            <?php endwhile;
            // Insert any content or load a template for no posts found.



The problem however is that this a Custom Post Type so I think I should be checking the taxonomy since the category is not working. I have already tried examples from this page http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters but to no avail.

Is there something else I am mean to be doing? Thanks

EDIT: This is the setup of my Custom Post Type

    add_action('init', 'register_products');
    function register_products() {
    register_post_type('products', array(
        'label' => 'Products',
        'description' => '',
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'capability_type' => 'post',
        'map_meta_cap' => true,
        'hierarchical' => false,
        'rewrite' => array('slug' => 'products', 'with_front' => true),
        'query_var' => true,
        'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes','post-formats'),
        'taxonomies' => array('category'),
        'labels' => array (
          'name' => 'Products',
          'singular_name' => 'Product',
          'menu_name' => 'Products',
          'add_new' => 'Add Product',
          'add_new_item' => 'Add New Product',
          'edit' => 'Edit',
          'edit_item' => 'Edit Product',
          'new_item' => 'New Product',
          'view' => 'View Product',
          'view_item' => 'View Product',
          'search_items' => 'Search Products',
          'not_found' => 'No Products Found',
          'not_found_in_trash' => 'No Products Found in Trash',
          'parent' => 'Parent Product',

Screenshot of the Dashboard: enter image description here

1 Answer 1


If you're actually using categories (and not a custom taxonomy) then your $categoryname variable needs to be the slug, not the actual name. Admittedly this Wp_Query argument name is confusing.

If, however, you are using a custom taxononomy (and you'll know if you are) then you should use

'name-of-your-custom-tax' => 'slug'


The codex is your friend: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • Thanks Vancoder I've tried numerous options and still it is not filtering as I would expect it. Admittdely I normally set these CPTs manually but this time I wanted to see how the Custom Post Type UI plugin would work for me so I used this to create the CPT. I have since moved the function into the functions.php (in the EDIT above) and added a screenshot of my dashboard for the products. I've used examples out of both those links you sent me and neither have done the trick. Commented Mar 25, 2014 at 3:11
  • Wondering - could it be that in this case, "Apples" is a child of "Product"? Commented Mar 25, 2014 at 3:14

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.