0

I am working with a directory style wordpress theme that displays categories in masonry style columns with their posts listed under each category title. The categories are appearing on the front page in alphabetical order and I would like to change this preferably using advanced custom fields. I have managed to control the post order using acf just fine but am stumped as to how to change category order.

<?php
                    $home_cats = get_post_meta($post->ID, 'home_cats', true);
                    $select_categories = explode( ',', $home_cats );
                    foreach ( $select_categories as $cat ) {
                        $tax_query = array(
                            array(
                                'taxonomy' => 'category',
                                'field'    => 'id',
                                'terms'    => $cat,
                            ),
                        );
                        $term = get_term( $cat, 'category' );
                        $term_name = $term->name;
                        $args = array(
                            'posts_per_page'    => -1,
                            'post_type'         => 'post',
                            'tax_query'         => $tax_query,
                            'meta_key'       => 'placement',
                            'orderby'        => 'meta_value_num',
                            'order'          => 'ASC',
                        );
                        $the_query = new WP_Query( $args );
                        if( $the_query->have_posts() ) :
                        ?>

I assumed adding orderby and order fields to first tax_query array was the way to achieve this but it doesn't change a thing. Is it glaringly obvious to one of you experts where I am going wrong here? Thanks

  • 1
    the order of the categories seems to be as they are set within the custom field 'home_cats'. what is adding those categories to the custom field? – Michael Feb 2 at 21:25
  • Note that it's not a good idea to set -1 and grab infinitely many posts, set it to a high value you never expect to reach, that still loads, but never unlimited. Otherwise, how is that post meta value being set, and how is its order being set? Keep in mind if ACF knowledge is needed to answer this question then it would be offtopic – Tom J Nowell Feb 2 at 21:25
  • Thank you both, I realise now the theme creator used acf.php in includes and so perhaps this is indeed off topic and not straight forward wordpress question. You have pointed me in the right direction though. Also will change posts per page as you advised. – rstrauss Feb 2 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.