Support

Account

Home Forums Front-end Issues Query for each custom taxonomy in menu order

Helping

Query for each custom taxonomy in menu order

  • Hello,

    I’m trying to list event dates sorted by date asc and grouped by custom taxonomy.
    The result should be something like:

    TAXONOMY_1_NAME / TAXONOMY_1_DESC
    06.02.2016 ….
    20.02.2016 …

    TAXONOMY_2_NAME / TAXONOMY_2_DESC
    13.02.2016 ….
    24.02.2016 …

    My code is:

    
    	<ul id="events">
    
    <?php 
    
     $args = array( 'post_type'         =>  'termine',
                                    'posts_per_page'    =>  100,
                                    'orderby'           =>  'meta_value', 
                                    'order'             =>  'ASC',
                                    'meta_key'          =>  'event_date' ); 
     $query = new WP_Query( $args );
    
                              if ( $query->have_posts() ) {
                                        while ( $query->have_posts() ) {
                                            $query->the_post();
                                        
                                      $meta_date = get_field('event_date');
                                      $date = date("d.m.Y", strtotime($meta_date));
                                      $city = get_field('event_city');  
                                      $location  = get_field('event-location');     
                                      $title  = get_field('event_title');  
                                      $month  = date("n", strtotime($meta_date));
                                      $this_month = $monate[$month];
                                      $description = get_field('event_dec');
                                      $link_url = get_field('event_link_url');
                                      $link_text =  get_field('event_link_text');
    
                                      if (empty($link_text)) {
                                        $link_text = 'Link';
                                      }
                                    echo '<li>';
                                    echo '<h3>'.$meta_date.' '.$city.', '.$title.'</h3>';
                                    echo $description;
                                    if (!empty($link_url)) {
                                      echo '<a href="'.$link_url.'">'.$link_text.'</a>';
                                    }
                                    echo '</li>';
                                   
                                        }
    
                                    } else {
                                        echo 'Keine Termine vorhanden.';
                                    }
    
                                    // Restore original Post Data
                                    wp_reset_postdata();
                                    
                                     ?>
    
    </ul>

    How can I modify this query to get the event dates as described above?

    Best.
    P.

  • So if I understand correctly you want to order the posts by terms and then by date. There isn’t a way to order posts by term, you can read about this here and in the other urls that this links to. http://wordpress.stackexchange.com/questions/87639/wp-query-orderby-taxonomy-term-value-numeric

    The easiest way to accomplish the is to get the terms and then do a new query based with that includes a tax_query for each term.

    http://codex.wordpress.org/Function_Reference/get_terms

    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Query for each custom taxonomy in menu order’ is closed to new replies.