Support

Account

Home Forums Front-end Issues Sort custom term archive by ACF date

Solved

Sort custom term archive by ACF date

  • I have the following code for functions.php working for the post archives belonging to the whole taxes. It sorts their archive pages by date set in ACF field ‘release-date’.

    add_action( 'pre_get_posts', 'customize_game_tag_archive_query', 10 );
    
    function customize_game_tag_archive_query( $query ) {
      if ( $query->is_tax( array ('time', 'game_status', 'genre', 'publisher', 'developer', 'platform', 'gameplay') ) ) {
        // Sort posts by ACF release date.
        $query->set( 'order', 'DESC' );
        $query->set( 'orderby', 'meta_value_num' );
        // ACF date field value is stored like 20220328 (YYYYMMDD).
        $query->set( 'meta_key', 'release-date' );
      }
    }

    It works fine, but I have a term ‘early-access’ inside ‘game_status’ tax and I need its archive to be sorted by another ACF date field named early-access-release-date. How can I achieve this?

  • You cannot sort in WP with two different meta fields and have the posts intermingled based on the two fields. You also cannot sort post differently based on whether or not they have a specific term.

    My suggestion would be to create an acf/save_post action. In this action copy one of the two fields based on the post’s terms to a 3rd custom field using update_post_meta(). This field would not be editable in ACF. Then use this 3rd field to for post ordering.

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

You must be logged in to reply to this topic.