Replies: 0
Hey all!
I’ve got a question I want to do the following:
Generate page with all the posts sorted per category and then per custom field.
I’ve got the following code as a function for a shortcode.
Now I already know how to do this per category. I can split the posts per category and give is a nice title per category. But now I need to know how to do the same per pre-divined custom field. Say, “customfield_01”.
<?php
global $post;
$categories_array = array();
$thecategories = get_categories();
foreach( $thecategories as $category ){
$categories_array[] = $category->slug;
}
if($atts[ 'category' ]){
$atts[ 'category' ] = explode( ",", $atts[ 'category' ] );
}
//collect values, combining passed in values and defaults
$values = shortcode_atts(array(
'category' => ''
),$atts);
$categories = get_categories( array(
'orderby' => 'name',
'parent' => 0,
'slug' => $values['category']
) );
$current = get_the_ID($post->ID);
foreach ( $categories as $tax ) :
// List posts by the terms for a custom taxonomy of any post type
$args = array(
'post_type' => 'products',
'orderby' => 'ASC',
'posts_per_page'=>-1,
'category_name' => $tax->slug
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- magic -->
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endforeach; ?>
Looking at this code I get the feeling I first need to make an array with all the different values of field “customfield_01” of the current category.
Does anyone know how to do this?
Or maybe even more efficient? 🙂
Many many thanks for any suggestions !