custom post types - Why can't I filter my CPT shortcode by category?

I've created my own Custom Post Type called 'products'. I have also registered by own custom taxonomies f

I've created my own Custom Post Type called 'products'. I have also registered by own custom taxonomies for the CPT - a custom category type (so it is separate from the default wp posts) and also its own tag system.

Then I registered a shortcode for displaying the CPT. However I am having trouble filtering the display of the posts using the category and tag parameters.

UPDATE It appears as though my custom taxonomy 'type' was reserved and was not letting me output the CPT, it was defaulting back to generic posts. So I have updated my code to call it 'product_type' and the CPTs are displaying.

HOWEVER, I am not able to filter them via category or using the custom taxonomy:

e.g. [ss-products product_type="Bends" orderby="name" order="ASC"] If I attempt to filter, I return no results, I I use product_type="" it then displays all.

function ss_products_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Products', 'Post Type General Name', 'generatepress' ),
        'singular_name'       => _x( 'Product', 'Post Type Singular Name', 'generatepress' ),
        'menu_name'           => __( 'Products', 'generatepress' ),
        'parent_item_colon'   => __( 'Parent Product', 'generatepress' ),
        'all_items'           => __( 'All Products', 'generatepress' ),
        'view_item'           => __( 'View Product', 'generatepress' ),
        'add_new_item'        => __( 'Add New Product', 'generatepress' ),
        'add_new'             => __( 'Add New', 'generatepress' ),
        'edit_item'           => __( 'Edit Product', 'generatepress' ),
        'update_item'         => __( 'Update Product', 'generatepress' ),
        'not_found'           => __( 'Not Found', 'generatepress' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'generatepress' ),
    );

// Set other options for 'SS Products' Custom Post Type

    $args = array(
        'label'               => __( 'products', 'generatepress' ),
        'description'         => __( 'Ezyduct Products', 'generatepress' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'category', 'post_tag' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 8,
        'menu_icon'           => 'dashicons-cart',
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'capability_type'     => 'post',
    );

    // Registering your Custom Post Type
    register_post_type( 'products', $args );

}

/* Hook into the 'init' action so that the function
* containing our post type registrations are not 
* unnecessarily executed. 
*/
add_action( 'init', 'ss_products_post_type', 0 );

//create a custom taxonomy for the products
function ssproducts_custom_taxonomy() {

// Add new cats taxonomy, make it hierarchical like categories

  $cat_labels = array(
    'name' => _x( 'Product Type', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Types' ),
    'all_items' => __( 'All Product Types' ),
    'parent_item' => __( 'Parent Types' ),
    'parent_item_colon' => __( 'Parent Type:' ),
    'edit_item' => __( 'Edit Type' ), 
    'update_item' => __( 'Update Type' ),
    'add_new_item' => __( 'Add New Type' ),
    'new_item_name' => __( 'New Type Name' ),
    'menu_name' => __( 'Types' ),
  );

// Now register the custom products taxonomy

  register_taxonomy('product_type','products', array(
    'hierarchical' => true,
    'labels' => $cat_labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => true,
  ));

}

//hook into the init action and call plant_taxonomies when it fires
add_action( 'init', 'ssproducts_custom_taxonomy', 0 );


/* Create a shortcode for the Products CPT
* Enable shortcode to be filtered by category 
* Print the title/image and link to CPT 
*/

/* shortcode for listing specific pages as linked image tiles (filtered by category) */
function display_ssproducts($atts) {
    ob_start();

    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'products',
        'order' => 'date',
        'orderby' => 'title',
        'type' => '',
        'posts' => -1,
        'category' => '',
        'tag' => '',
    ), $atts ) );

    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'product_type' => $type,
        'category_name' => $category,
    );

    $query = new WP_Query( $options );
    // run the loop based on the query
    if ( $query->have_posts() ) { ?>
        <div class="pages-listing grid-container">
            <?php while ($query->have_posts()) : $query->the_post(); ?>
                <div id="post-<?php the_ID(); ?>" class="grid-20 tablet-grid-25 mobile-grid-50 clearfix">
                    <div class="item-cont">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php if (has_post_thumbnail() ): ?>
                            <div class="product-img-wrapper">
                              <?php the_post_thumbnail('medium'); ?>
                            </div>
                        <?php endif; ?>
                        <p><?php the_title(); ?></p>
                    </a>
                    </div>
                </div>
            <?php endwhile;
            wp_reset_postdata();
            ?>
        </div>

    <?php } ?>

    <script>
    jQuery(document).ready(function($) {
      var maxHeight = 0;
      $(".grid-25 .item-cont").each(function() {
        if ($(this).height() > maxHeight) {
          maxHeight = $(this).height();
        }
      }).height(maxHeight);
    });
    </script>

<?php         
    $custom_page_display = ob_get_clean();
    return $custom_page_display;
}

/* Initiate the programs CPT shortcode on startup (init) */
add_action( 'init', 'register_ssproducts_shortcode');
function register_ssproducts_shortcode(){
   // Add shortcode to produce product links as tiles (filter by cat)
   add_shortcode('ss-products', 'display_ssproducts'); 
}

I've created my own Custom Post Type called 'products'. I have also registered by own custom taxonomies for the CPT - a custom category type (so it is separate from the default wp posts) and also its own tag system.

Then I registered a shortcode for displaying the CPT. However I am having trouble filtering the display of the posts using the category and tag parameters.

UPDATE It appears as though my custom taxonomy 'type' was reserved and was not letting me output the CPT, it was defaulting back to generic posts. So I have updated my code to call it 'product_type' and the CPTs are displaying.

HOWEVER, I am not able to filter them via category or using the custom taxonomy:

e.g. [ss-products product_type="Bends" orderby="name" order="ASC"] If I attempt to filter, I return no results, I I use product_type="" it then displays all.

function ss_products_post_type() {

// Set UI labels for Custom Post Type
    $labels = array(
        'name'                => _x( 'Products', 'Post Type General Name', 'generatepress' ),
        'singular_name'       => _x( 'Product', 'Post Type Singular Name', 'generatepress' ),
        'menu_name'           => __( 'Products', 'generatepress' ),
        'parent_item_colon'   => __( 'Parent Product', 'generatepress' ),
        'all_items'           => __( 'All Products', 'generatepress' ),
        'view_item'           => __( 'View Product', 'generatepress' ),
        'add_new_item'        => __( 'Add New Product', 'generatepress' ),
        'add_new'             => __( 'Add New', 'generatepress' ),
        'edit_item'           => __( 'Edit Product', 'generatepress' ),
        'update_item'         => __( 'Update Product', 'generatepress' ),
        'not_found'           => __( 'Not Found', 'generatepress' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'generatepress' ),
    );

// Set other options for 'SS Products' Custom Post Type

    $args = array(
        'label'               => __( 'products', 'generatepress' ),
        'description'         => __( 'Ezyduct Products', 'generatepress' ),
        'labels'              => $labels,
        // Features this CPT supports in Post Editor
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'category', 'post_tag' ),
        'hierarchical'        => true,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 8,
        'menu_icon'           => 'dashicons-cart',
        'can_export'          => true,
        'has_archive'         => false,
        'exclude_from_search' => true,
        'capability_type'     => 'post',
    );

    // Registering your Custom Post Type
    register_post_type( 'products', $args );

}

/* Hook into the 'init' action so that the function
* containing our post type registrations are not 
* unnecessarily executed. 
*/
add_action( 'init', 'ss_products_post_type', 0 );

//create a custom taxonomy for the products
function ssproducts_custom_taxonomy() {

// Add new cats taxonomy, make it hierarchical like categories

  $cat_labels = array(
    'name' => _x( 'Product Type', 'taxonomy general name' ),
    'singular_name' => _x( 'Type', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Types' ),
    'all_items' => __( 'All Product Types' ),
    'parent_item' => __( 'Parent Types' ),
    'parent_item_colon' => __( 'Parent Type:' ),
    'edit_item' => __( 'Edit Type' ), 
    'update_item' => __( 'Update Type' ),
    'add_new_item' => __( 'Add New Type' ),
    'new_item_name' => __( 'New Type Name' ),
    'menu_name' => __( 'Types' ),
  );

// Now register the custom products taxonomy

  register_taxonomy('product_type','products', array(
    'hierarchical' => true,
    'labels' => $cat_labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => true,
  ));

}

//hook into the init action and call plant_taxonomies when it fires
add_action( 'init', 'ssproducts_custom_taxonomy', 0 );


/* Create a shortcode for the Products CPT
* Enable shortcode to be filtered by category 
* Print the title/image and link to CPT 
*/

/* shortcode for listing specific pages as linked image tiles (filtered by category) */
function display_ssproducts($atts) {
    ob_start();

    // define attributes and their defaults
    extract( shortcode_atts( array (
        'type' => 'products',
        'order' => 'date',
        'orderby' => 'title',
        'type' => '',
        'posts' => -1,
        'category' => '',
        'tag' => '',
    ), $atts ) );

    // define query parameters based on attributes
    $options = array(
        'post_type' => $type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'product_type' => $type,
        'category_name' => $category,
    );

    $query = new WP_Query( $options );
    // run the loop based on the query
    if ( $query->have_posts() ) { ?>
        <div class="pages-listing grid-container">
            <?php while ($query->have_posts()) : $query->the_post(); ?>
                <div id="post-<?php the_ID(); ?>" class="grid-20 tablet-grid-25 mobile-grid-50 clearfix">
                    <div class="item-cont">
                    <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                        <?php if (has_post_thumbnail() ): ?>
                            <div class="product-img-wrapper">
                              <?php the_post_thumbnail('medium'); ?>
                            </div>
                        <?php endif; ?>
                        <p><?php the_title(); ?></p>
                    </a>
                    </div>
                </div>
            <?php endwhile;
            wp_reset_postdata();
            ?>
        </div>

    <?php } ?>

    <script>
    jQuery(document).ready(function($) {
      var maxHeight = 0;
      $(".grid-25 .item-cont").each(function() {
        if ($(this).height() > maxHeight) {
          maxHeight = $(this).height();
        }
      }).height(maxHeight);
    });
    </script>

<?php         
    $custom_page_display = ob_get_clean();
    return $custom_page_display;
}

/* Initiate the programs CPT shortcode on startup (init) */
add_action( 'init', 'register_ssproducts_shortcode');
function register_ssproducts_shortcode(){
   // Add shortcode to produce product links as tiles (filter by cat)
   add_shortcode('ss-products', 'display_ssproducts'); 
}
Share Improve this question edited Oct 6, 2019 at 3:20 Ryan Coolwebs asked Oct 5, 2019 at 20:16 Ryan CoolwebsRyan Coolwebs 6892 gold badges5 silver badges18 bronze badges 2
  • post_tag is a built-in taxonomy in WordPress. Why are you registering it? – Sally CJ Commented Oct 6, 2019 at 1:19
  • @SallyCJ, Not sure. I thought I had to register it after following some tutorials on the net. Yes, I can see that was a mistake now, so I have edited the code. Still not helping with my core issue though. – Ryan Coolwebs Commented Oct 6, 2019 at 3:08
Add a comment  | 

1 Answer 1

Reset to default 0

My biggest mistake was using a taxonomy named 'type'. Yes it messed everything up since it caused many conflicts (with soft fails, no error messages). I'm doing this on my localhost but somehow some changes did not come through instantly - had to log off, close and login again to see desired results.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745103607a4611425.html

相关推荐

  • custom post types - Why can&#39;t I filter my CPT shortcode by category?

    I've created my own Custom Post Type called 'products'. I have also registered by own custom taxonomies f

    9小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信