• Hi,
    I would like to get an array of all the category IDs for each store so that I can check them against a ‘preferred category’ saved in a users profile.
    I am unable to get an array of the term_ids – I am using this function to succesfully display the IDs in the store template.

    function custom_store_meta1( $store_meta, $store_id ) {
    
    	$terms = get_the_terms( $store_id, 'wpsl_store_category' );
    
    	$store_meta['terms'] = '';
    
    	if ( $terms ) {
    		if ( ! is_wp_error( $terms ) ) {
    			if ( count( $terms ) > 1 ) {
    				$location_terms = array();
    
    				foreach ( $terms as $term ) {
    					$location_terms[] = $term-><strong>term_id</strong>;
    				}
    
    				$store_meta['terms'] = implode( ',', $location_terms );
    			} else {
    				$store_meta['terms'] = $terms[0]-><strong>term_id</strong>;
    			}
    		}
    	}
    
    	return $store_meta;
    }

    In the custom template I am making an array of the term IDs like this:

        $terms_array = '<%= terms %>';
        $terms_array = explode(',', $terms_array);
        print_r($terms_array);

    The result is Array ( [0] => 31,22,25 ) which is not a proper array…

    In summary – how could I get an array of the store’s terms/categories from within the custom store template?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter southafricanrob

    (@southafricanrob)

    Hi again,
    I realise I was going about this back to front and all the IDs can be obtained and used inside the custom_store_meta function, and then the result passed to the custom store template.

    Much simpler and it works.;-)

    Hi there,

    It is great you figured it out, I didn’t have time to reply in the first place ??

    Regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get Array of category IDs’ is closed to new replies.