• Resolved cpac7

    (@cpac7)


    I’m sure this has been asked a bunch of times, but nothing I’ve researched has made much sense.

    I’m creating a specialized product listing for libraries sorted by a Custom Category Taxonomy and then a regular WP category within a seasonal trend archive page.

    I have a CPT pod called product that is associated with a custom category called season (Winter, Spring, Summer and Fall).

    This same CPT pod is also associated with another custom taxonomy category called library_type (Municipal Libraries, School Libraries, College Libraries, etc…).

    These CPT products are then associated with the built in WordPress categories (fiction, non-fiction, children’s, etc…)

    I already have the archive template built that lists the CPT products based on whatever season the user clicks on. (Winter, Spring, Summer, Fall). This part is done and works great!

    What I’m having trouble with is how to sort the products by the other two categories. As an example:

    SUMMER ARCHIVE

    Municipal Libraries

    Fiction
    Book One
    Book Two
    Book Three

    School Libraries

    Non-Fiction
    Book Four
    Book Five

    I am building this template with php. Any help would be appreciated. Thank You.

Viewing 1 replies (of 1 total)
  • Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @cpac7

    You’ll need to sort your product by using loops.
    NOTE: The code below is not complete, it’s meant as an example.

    1. Cache all taxonomies so later on they are already loaded.

    
    $seasons    = get_terms( array( 'taxonomy' => 'your_seasons_tax' ) );
    $libraries  = get_terms( array( 'taxonomy' => 'your_libraries_tax' ) );
    $categories = get_terms( array( 'taxonomy' => 'your_category' ) );
    

    1. Fetch the products you need.

    
    $products = your_get_products_function();
    

    2. Loop through all products, creating a multidimensional product list for each taxonomy level (season, etc.)

    
    $list = array();
    foreach ( $products as $product ) {
    	$product_seasons    = get_the_terms( get_the_ID(), 'your_seasons_tax' );
    	$product_libraries  = get_the_terms( get_the_ID(), 'your_libraries_tax' );
    	$product_categories = get_the_terms( get_the_ID(), 'your_category' );
    	foreach ( $product_seasons as $season ) {
    		$season_id = $season->term_id;
    		if ( ! isset( $list[ $season_id ] ) ) {
    			$list[ $season_id ] = array();
    		}
    		foreach ( $product_libraries as $library ) {
    			$library_id = $library->term_id;
    			if ( ! isset( $list[ $season_id ][ $library_id ] ) ) {
    				$list[ $season_id ][ $library_id ] = array();
    			}
    			foreach ( $product_categories as $category ) {
    				$category_id = $category->term_id;
    				if ( ! isset( $list[ $season_id ][ $library_id ][ $category_id ] ) ) {
    					$list[ $season_id ][ $library_id ][ $category_id ] = array();
    				}
    				// Add the product
    				$list[ $season_id ][ $library_id ][ $category_id ][] = $product;
    			}
    		}
    	}
    }
    

    After that you can start a multi-level loop for display.

    You could also do it the other way around. Looping through all existing taxonomies and then getting the related products for each combination though I think this is slower.
    For more info, see the tax_query and this example: https://wordpress.stackexchange.com/questions/155358/grab-all-custom-posts-by-multiple-taxonomies-and-terms

    Good luck!

    Cheers, Jory

    • This reply was modified 3 years, 8 months ago by Jory Hogeveen.
Viewing 1 replies (of 1 total)
  • The topic ‘Categorize Pod CPT by Custom Categories and Categories’ is closed to new replies.