Search code examples
wordpresscustom-post-typeadvanced-custom-fieldstaxonomytaxonomy-terms

Custom post type, sort taxonomy terms by a meta box value


I've have created a custom post type with a taxonomy. I need to sort the taxonomy posts by a meta value in my custom post type. I am using the plugin Advanced Custom Fields to insert a separate meta value field to my posts in my custom post types. So far I've been able to get posts taxonomies sort by its value. My problem is that I get "all" of my taxonomies post printing out on the front-end. I would like to get the posts for each taxonomies it's related to, not ALL. I've been testing to get the term id by implement it on the wp_query array but with no success. I simply want to display my taxonomies individually and grab its related posts, is this possible, what am I doing wrong?

Here's my code...

    <?php 
get_header(); 

if( have_posts() ) { 
?>
    <section class="section container">
        <div class="row">
<?php       
            // gets our taxonomy title      
            global $wp_query; 
            $term = $wp_query->get_queried_object();
            $title = $term->name; 
?>
            <!-- show our taxonomy title on the front-end of the site -->
            <header id="<?php echo $term->slug;?>" class="col-md-12">   
                <h1><a href="<?php bloginfo( 'url' ); ?>/?p=493"><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span></a><?php echo $title; ?></h1>                            
            </header>`enter code here`
<?php              

        // wp_query-loop for our custom post typ "Personal", sort by meta_value 
        $wp_query = new WP_Query(array(
            'post_type'         => 'Personal',
            'posts_per_page'    => -1,
            'meta_key'          => 'sorterings_nummer',
            'orderby'        => '$term->term_id meta_value',
            'ordertax'       => 'DESC',
            'order'          => 'ASC'
        ));

        // gets our custom post type posts with a thumbnail, title and contact details  
        while( $wp_query->have_posts() ) {
            $wp_query->the_post(); 

            $class = get_field('sorterings_nummer') ? 'class="sorterings_nummer"' : '';

            $titel = get_post_meta(get_the_ID(), 'titel');
            $telefon = get_post_meta(get_the_ID(), 'telefon');
            $mobil = get_post_meta(get_the_ID(), 'mobil');
            $mail = get_post_meta(get_the_ID(), 'mail');
            add_filter('the_content', 'wpautop');
?>
            <article class="col-xs-6 col-sm-4 col-md-4 col-lg-3" >
                <div class="align-center">
                    <div class="content--thumbnail">
                        <?php the_post_thumbnail(); ?>
                    </div>

                    <header class="content--title">
                        <h2><?php the_title(); ?></h2>
                    </header>

                    <section class="content--contactDetails">
                        <h3 class="titel"><?php echo $titel[0] ? $titel[0]: ''; ?></h3>  
                        <p class="telefon"><strong><a href="tel:<?php echo $telefon[0] ?>"><?php echo $telefon[0] ? $telefon[0]: ''; ?></strong></a></p>
                        <p>
                            <a class="mail" href="mailto:<?php echo $mail[0] ?>"><?php echo $mail[0] ? $mail[0]: ''; ?></a>
                            <a class="mobil" href="tel:<?php echo $mobil[0] ?>"><?php echo $mobil[0] ? $mobil[0]: ''; ?></a>
                        </p>
                    </section>    
                </div>
            </article>
<?php 
        } // while content
?>
        </div> <!-- .row -->
    </section> <!-- .container -->
<?php 
} // if 
get_footer(); 

Solution

  • Please try tax_query() for retrieving posts assigned to the particular taxonomy term.

    Please find your updated $wp_query() code below:

    $wp_query = new WP_Query(array(
                'post_type'         => 'Personal',
                'posts_per_page'    => -1,
                'tax_query'         => array(array('taxonomy' => 'taxonomy_slug_name', 'field' => 'id', 'terms' => $term->term_id )),
                'meta_key'          => 'sorterings_nummer',
                'orderby'           => 'meta_value',
                'ordertax'          => 'DESC',
                'order'             => 'ASC'
            ));
    

    Please write your 'taxonomy_slug_name' in the taxonomy column of tax_query().

    Hope, this may be helpful to you.