• Resolved Philip Jones

    (@philipmjones)


    I am using CFS with a custom post type (‘writers’) which includes a related field, so each writer can state which other writers they are similar to. To show a complete list of who is related to a given writer, I combine two lookups: a simple lookup of their ‘related’ field and a reverse lookup of other writers and their ‘related’ fields.

    By default get() and get_reverse_related are returning all ‘writer’ records including those that are draft or in trash. I need to filter this to just return those that are published.

    With get_reverse_related this is fine:

    CFS()->get_reverse_related($post->ID, array(‘field_name’ => ‘similar_to’, ‘post_status’ => ‘publish’));

    However I cannot work out how to filter the output of get() in a similar way so that it returns just published ‘writers’.

    Please can you help, or give me some pointers?

    Thanks,

    Philip

    https://www.ads-software.com/plugins/custom-field-suite/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Matt Gibbs

    (@mgibbs189)

    Hey, that’s correct, that method will return all matching post IDs, regardless of its post status.

    To get around this issue, you’ll need to then pass those post IDs through WP_Query to get only the ones you want. E.g.

    $related_ids = CFS()->get_reverse_related( $post->ID, array( 'field_name' => 'similar_to' ) );
    $related_ids = empty( $related_ids ) ? array( 0 ) : $related_ids;
    
    $query = new WP_Query( array(
      'post_status' => 'publish',
      'post_type' => 'any',
      'post__in' => $related_ids,
      'fields' => 'ids',
    ) );
    
    // the good array of post ids
    $final_post_ids = $query->posts;
    
    Thread Starter Philip Jones

    (@philipmjones)

    Hi Matt,

    Thanks very much for sorting that out for me.

    Have a great day,

    Philip

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get() is returning unpublished/trashed items’ is closed to new replies.