Support

Account

Home Forums General Issues orderby date picker field in get_terms

Solving

orderby date picker field in get_terms

  • Background:
    -I’ve got a magazine site with an ‘Issues’ taxonomy, which gets another issue added to it every month. I want to order many things around my site by this issue tax but am unable to do so.

    Scenario 1:
    I want to display all issues of the magazine on a page in descending order by the date picker field on a simple page. I have taxonomy filtering setup with a plugin that rearranges my taxonomy but that is not ideal – non-technical people keep adding issues, it should automatically sort whatever they add in.

      $editions_terms = get_terms( array(
        'taxonomy' => 'issues',
        'order' => 'DESC',
        'hide_empty' => 0
      ) );
    

    Scenario 2:

    I want to display the latest 3 custom post-type items by taxonomy issue publication date. I have no idea how to do this – best I can figure is to order my custom post-type by ID of publication.

         $args = array(
            'posts_per_page' => 3,
            'orderby' => 'ID',
            'post_type' => array( 'departments' ),
            'tax_query' => array(
              array(
                'taxonomy' => 'department',
                'field'    => 'slug',
                'terms'    => 'federal-file'
              )
            )
          );
          $the_query = new WP_Query( $args );

    I’ve been reading a lot of random blog posts and S.O. stuff but nothing seems to work just right for what I’m doing. Am I missing something?

  • Scenario 1

    If I’m understanding correctly, no, you cannot do this. You want to reorder the “terms” in a taxonomy based on a date edited when adding a “post”. You can’t order the terms according to something associated with the posts in those terms. To do this you would need to build something custom to somehow record the most recent date of all of the posts in a term and store that information in a way that is related to the term so that you can then use the date added to the term to order the terms.

    1. acf/save_post priority > 10 https://www.advancedcustomfields.com/resources/acf-save_post/
    2. get the term of the post being saved
    3. get the post in the term with the most recent date
    4. update the term using term meta with this date https://codex.wordpress.org/Function_Reference/update_term_meta
    5. add a meta_query to your get terms call to sort them by this new term meta key

    Scenario 2

    You already have most of this, you just need to add a meta_key and order by the meta key in your query. https://www.advancedcustomfields.com/resources/orde-posts-by-custom-fields/

  • Ordering terms in WP is tough. For the moment I’ve gone with the solid plugin,
    Custom Taxonomy Order NE. If I have time, I’ll likely pursue something along the lines of what you’ve laid out and update this post with progress and potential workaround for others.

    But if you can order your taxonomy with the above plugin it allows you inefficiently get an archive of posts by tax term. You can create a series of WP_Query calls that run through each term. Use get_terms() to create an array of all tax terms, then run a foreach over each term. This creates a WP_Query for each term item that will return all posts for a given term. Code to make this happen:

      // Get your terms and put them into an array
      $issue_terms = get_terms([
        'taxonomy' => 'issues',
        'hide_empty' => false,
      ]);
    
      // Run foreach over each term to setup query and display for posts
      foreach ($issue_terms as $issue_term) {
        $the_query = new WP_Query( array(
          'post_type' => 'post',
          'tax_query' => array(
            array(
              'taxonomy' => 'issues',
              'field' => 'slug',
              'terms' => array( $issue_term->slug ),
              'operator' => 'IN'
            )
          )
        ) );
    
        // Run loop over each query
        while($the_query->have_posts()) :
          $the_query->the_post();
    
          // YOUR TEMPLATE OUTPUT FOR EACH POST
    
        endwhile;
      }
  • If the value you wanted to order them by was already saved in term meta it would be easy. The difficulty is really that what you really want to order the terms by has something to do with the posts in those terms.

    It might also be possible to construct (a very complicated and over my head) MySQL query and use $wpdb to do the work.

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

The topic ‘orderby date picker field in get_terms’ is closed to new replies.