• Resolved CharlyIBC

    (@charlyibc)


    Preface: This is my first time with WP and Woo and my site isn’t live yet. I’ve spent hours/days trying to figure this out. 🙁

    Mine is a photography site. Galleries has all product categories listed in the sub-menu and shows the appropriate thumbs. I created a page called Projects to which I want to show them separately from the Galleries page. But when I added the category for my current project it shows up in Galleries, even though it’s not in the sub-menu.

    I found this snippet that I’m supposed to use:

    add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
     
    function custom_pre_get_posts_query( $q ) {
     
    	if ( ! $q->is_main_query() ) return;
    	if ( ! $q->is_post_type_archive() ) return;
    
    	if ( ! is_admin() && is_shop() ) {
     
    		$q->set( 'tax_query', array(array(
    			'taxonomy' => 'product_cat',
    			'field' => 'slug',
    			'terms' => array( 'knives' ), // Don't display products in the knives category on the shop page
    			'operator' => 'NOT IN'
    		)));
    
    	}
     
    	remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
     
    }

    But it doesn’t work and gives me the dreaded white screen. I cannot figure out what I’m doing wrong. I changed in_shop to in_galleries as I changed it’s name. I put in the slug with hyphens. I tried putting in the multi-word name in terms with spaces, hyphens and underscores, no joy either way.

    I looked in Firebug to see if I could find a post #, product #, category #, or whatever for that specific one, but no joy either.

    I would greatly appreciate any help with this as it’s the only thing (outside of adding more prints) that is keeping me from going live. Thanks in advance for any assistance!

    WooTheme – Mystile and WooCommerce are up to date

Viewing 9 replies - 1 through 9 (of 9 total)
  • Thread Starter CharlyIBC

    (@charlyibc)

    I found a code that worked for me, finally solved!

    celestinaadams

    (@celestinaadams)

    Glad to hear you found a solution! I have something similar going on, would you mind posting the solution you found? Thanks!

    Thread Starter CharlyIBC

    (@charlyibc)

    I cannot say this will work for you, but of all I tried, it did the trick for me. I’m using WooTheme Mystile and WooCommerce 2.1.2 with WP 3.8.1

    Under the custom Theme Functions.php or your childtheme functions.php place this code:

    /* Exclude Category from Shop*/
    
    add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
    
    function get_subcategory_terms( $terms, $taxonomies, $args ) {
    
      $new_terms = array();
    
      // if a product category and on the shop page
      if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
    
        foreach ( $terms as $key => $term ) {
    
          if ( ! in_array( $term->slug, array( 'project1', 'susan' ) ) ) {
            $new_terms[] = $term;
          }
    
        }
    
        $terms = $new_terms;
      }
    
      return $terms;
    }

    The only thing you need to replace is ‘project1‘, ‘susan‘ Now these are 2 categories that are not shown on a shop page and the slug names for the categories removed. For instance if you want to remove category1, look up the slug you used and put that in single quotes. If it’s category 1 then this is the slug code you’d use: ‘category1’

    If you just want 1 cat., then remove the comma ‘susan’ or if you want more removed, just add a comma after ‘susan’, ‘newcategory’, ‘anothercategory’ and so on. Hope this works for you!

    Hi,

    I’m after something similar to this. I don’t want a category showing up on the ‘shop’ page, but I want it to show up in the sidebar widget area. Any idea how to go about that?

    Many thanks!

    Thread Starter CharlyIBC

    (@charlyibc)

    Have you checked out the documentation on the WooThemes site? If memory serves, I saw something about a category widget for your sidebar. Remember unless you have product in a category (1 item will do), it won’t show up any where.

    Hi CharlylBC,

    Thanks for replying. I’ve added the code you gave above which worked successfully to hide my ‘archive’ category. I’ve got a WooCommerce product category widget installed that’s showing all the other shop categories bar the archive. There are about 5 items in the archive so I don’t see why it wouldn’t show up.

    Thread Starter CharlyIBC

    (@charlyibc)

    Hi and you’re welcome. Honestly I’m pretty new to all of this and it took weeks to figure out the above code. My guess is something in that function might be causing it, but I’m not sure

    If you post a link to a page with the category widget, I’ll take a look when I have a moment. Albeit no guarantees if I’ll be able to help you out.

    For some reason – I cleared my widget cache and browser cache must be a dozen times prior – the archive category has appeared! Yay, all is well! 🙂

    Hi Thanks for this.

    My products that i want to add are in two categories. e.g. bespoke and bracelets.

    How do i perform this php insert to hide if it picks up the “bespoke” category?

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘WooCommerce – exclude category from shop’ is closed to new replies.