Support

Account

Home Forums Front-end Issues Relationship – splitting results by post type

Solving

Relationship – splitting results by post type

  • Is it possible to filter the results from the relationship field on the front end website so that they display by post type and/or taxonomy rather than just a list of all selected items.

    So on the front end we would end up with a

      for each of the post types and taxonomies that were selected in the admin.

      Custom Post Type 1
      Post 1
      Post 2
      Post 3

      Custom Post Type 2
      Post 4
      Post 5

      Custom Taxonomy 1
      Post 6
      Post 7

      Custom Taxonomy 2
      Post 8
      Post 9

  • I’ve done this with get_post_type():

    <?php
    
    $rel_field_ids = get_field( 'rel_field' );
    $rel_field_ids_grouped = array();
    
    foreach ( $rel_field_ids as $rel_field_id )
    {
    	$post_type = get_post_type( $rel_field_id );
    	$rel_field_ids_grouped[ $post_type ][] = $rel_field_id;
    }
    
    ksort( $rel_field_ids_grouped );
    print_r( $rel_field_ids_grouped );
    
    ?>

    Code not tested.

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

The topic ‘Relationship – splitting results by post type’ is closed to new replies.