• Hello,

    I’m working on a website where I have to add products to. I created a Custom Post Type called: Products. The thing I want is to filter these products by categories. In my sidebar you can see the Product Categories. On my main Products page all products (currently 2) are displayed.

    Now the problem. When I click on one of these categories, it’s going to the right URL but it’s not displaying the product in it (both categories have one of the products in it). Instead it displays the Index.

    URL: Click here >

    Here is the code I have in my archive-products.php:

    <?php
    /*
    Template Name: Products
    */
    get_header();
    ?>
    
    <div class="pageBox">
        <div class="inner">
    
            <div class="content">
    
                <h2><?php the_title(); ?></h2>
    
    		<ul class="products">
    
    		<?php
                        $args = array(
                            'post_type'       => 'products',
                            'posts_per_page'  => 9,
                            'paged' 	  =>  $paged,
                            'orderby'         => 'post_date',
                            'order'           => 'DESC',
                            'has_archive'     => true,
    		        'taxonomy'	  => 'products_category'
                        );
    
                        $loop = new WP_Query($args);
                        while ( $loop->have_posts() ) : $loop->the_post();
    
                        $product_img = wp_get_attachment_image_src(get_field('product_image'), 'Product Image');
    
                ?>
    
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <img src="<?php echo $product_img[0]; ?>" alt="" />
                    </a>
                    <a href="<?php the_permalink(); ?>">
                        <h2><?php the_title(); ?></h2>
                    </a>
                    <p><?php echo get_field('product_description'); ?></p>
                </li>
    
                <?php endwhile; ?>
    
            </ul>
    
            </div>
    
         	<div class="sidebar">
    
                <?php if(!function_exists("dynamic_sidebar") || !dynamic_sidebar("Sidebar Products")): ?><?php endif; ?>
    
            </div>   
    
        </div>
    </div>
    
    <?php get_footer(); ?>

    And here the CPT in my functions.php:

    function products_post_type() {
    
    	register_post_type( 'products',
    		array(
    			'labels' => array(
    				'name' => __( 'Products' ),
    				'singular_name' => __( 'Products' )
    			),
    			'taxonomies' => array(
    					   'products_category'
    		    ),
    			'public' => true,
    			'show_ui' => true,
    			'has_archive' => true,
    			'hierarchical' => true,
    			'rewrite' => array('slug' => 'products'),
    			'query_var' => true,
    			'supports' => array(
    					'title',
    					'custom-fields',
    					'editor',
    					'thumbnail',
    					'taxonomies',
    					'category'
    					)
    				)
    			);
    
    		register_taxonomy('products_category','Categorie',
    			array(
    			   'sort' => true,
    			   'args' => array( 'orderby' => 'term_order' ),
    			   'hierarchical' => true,
    			   'rewrite' => array( 'slug' => 'products' ),
    			   'labels' => $labels
    			));                                                                  
    
    		register_taxonomy_for_object_type('products_category', 'products');
    
    }
    
    add_action( 'init', 'products_post_type' ,1);

    Any help is welcome!
    Thanks in advance.

Viewing 15 replies - 1 through 15 (of 17 total)
  • Hi, change your archive-products.php file name to taxonomy.php file name, and remove the commented text at top.

    Thread Starter nataschakater

    (@nataschakater)

    Hi, thanks for your reply.
    Unfortunately my product page is now redirecting to Index. Category pages too. Already tried to flush my permalinks but no luck.

    Thread Starter nataschakater

    (@nataschakater)

    When I set my permalinks to Default the product page is showing the products correctly but the categories also show BOTH products (the title gives the product name of the product in that category). Changing the permalinks back to Post name gives me Index again.

    Sultan

    (@sultan_semmaiyahoocom)

    What is your problem your single product is not displaying?

    if yes then create a file with name single-product.php and show the single product there.

    i have visit your link i think you have problem with your single product?

    Sultan

    (@sultan_semmaiyahoocom)

    if your categories not showing the product just create a file in your theme with the name category.php and put your code there.

    Thread Starter nataschakater

    (@nataschakater)

    Hi there,

    No the single is working fine. The problem is that my category pages use the Index. I just created a category.php but no luck.

    Sultan

    (@sultan_semmaiyahoocom)

    try to put some code in category.php

    like

    <?php $loop = new WP_Query( array( ‘post_type’ => ‘product’, ‘posts_per_page’ => ’10’, ‘category_name’ => single_cat_title(”, false)) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    // some content of loop goes here
    <?php endwhile; ?>

    Thread Starter nataschakater

    (@nataschakater)

    Yeah well… I can put some code in category.php but I can’t see the category.php on the frontend so why add some code?

    – Added that code, nothing changes on frontend. Categories still using Index.

    Thanks for your help btw!

    Sultan

    (@sultan_semmaiyahoocom)

    try this one

    taxonomy-products.php

    hope this work for you.

    Thread Starter nataschakater

    (@nataschakater)

    Nope, still index.

    Sultan

    (@sultan_semmaiyahoocom)

    Try this one
    taxonomy-products_category.php

    mean taxonomy-your taxonomy name.php

    Hi @nataschakater, cpts can be tricky, alright.

    Try registering your ‘products_category’ into something like ‘types’.

    I may be recommending this in vain, but digging further into the documentation may solve your problem: http://codex.wordpress.org/Post_Types

    Thread Starter nataschakater

    (@nataschakater)

    Ok, I got something…

    I changed the rewrite in my Custom Post Type to ‘productitem’ and the file to taxonomy-products_category.php. Now the category page is actually showing the category page!

    But… I’m not there yet. If you go to a category page now you can see that both products are in there. There should only be one in there.

    Here is the code I used:

    <ul class="products">
    
    <?php
        $args = array(
            'post_type' 		=> 'products',
            'posts_per_page' 	=> 9,
            'paged' 		=>  $paged,
            'orderby'       	=> 'post_date',
            'order'         	=> 'DESC',
            'has_archive' 		=> true,
    	'taxonomy'		=> 'products_category'
        );
    
        $loop = new WP_Query($args);
        while ( $loop->have_posts() ) : $loop->the_post();	
    
        $product_img = wp_get_attachment_image_src(get_field('product_image'), 'Product Image');
    
    ?>
    
    <li>
        <a href="<?php the_permalink(); ?>">
            <img src="<?php echo $product_img[0]; ?>" alt="" />
        </a>
        <a href="<?php the_permalink(); ?>">
            <h2><?php the_title(); ?></h2>
        </a>
        <p><?php echo get_field('product_description'); ?></p>
    </li>
    
    <?php endwhile; ?>
    
    </ul>

    Same as my archive-products.php. It should filter them but I’m not sure which code to use.

    Thanks.

    Thread Starter nataschakater

    (@nataschakater)

    Hi @georgedk,

    Thanks for your reply. I changed ‘products_category’ to ‘types’ but nothing changed. Still the problem of above. It’s not filtering the products by category.

    Sultan

    (@sultan_semmaiyahoocom)

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Custom Post Type with products by Category problem’ is closed to new replies.