1

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(); 
4

1 回答 1

1

请尝试 tax_query() 检索分配给特定分类术语的帖子。

请在下面找到您更新的 $wp_query() 代码:

$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'
        ));

请在 tax_query() 的分类列中写下您的“taxonomy_slug_name”。

希望,这可能对您有所帮助。

于 2017-02-16T06:27:59.047 回答