Skip Navigation

[Resolved] Filter reference fields by its custom field

This support ticket is created 5 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Shane 5 years, 4 months ago.

Assisted by: Shane.

Author
Posts
#1158703

Tell us what you are trying to do?
Hi, I have a CPT "Group" that has a reference post field "Members". Everything is working fine. Members CPT has a custom field status ('active', 'resigned' etc.). I want to filter Members by its custom field 'status'. I have seen this link where Chirstian described how can we filter reference post type in an add form using page id or page slug. https://toolset.com/forums/topic/only-show-current-users-posts-in-post-reference-field/

1. I want to use the same in page edit form as well that is called in a content template, should I just simply use the content template id? (I tried by with no results)
2. I want to filter reference post by its own custom field, what I have to add in query?

Thanks,

Khurram

#1158798

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Khurram,

Thank you for contacting our support forum.

So christian used this code.

* --------------------------------------------- */
// FILTER POST REFERENCE FIELD
// Filter the posts in a post reference field by author, where the author is the current User
function filter_reference_posts( $query ){
 
    $target_page = 12345; // ID of page with form, or string of page slug
    $ref_post_type = "cpt-slug"; // slug of post type in the post reference field
    if ( ( is_page( $target_page ) || ( defined('DOING_AJAX') && DOING_AJAX ) ) && is_array($query->get( 'post_type')) && in_array($ref_post_type, $query->get('post_type')) ) {
        $current_user = get_current_user_id();
 
        $query->set( 'author', $current_user );
    }
}
add_action( 'pre_get_posts', 'filter_reference_posts' );

If this form is on a content template you will need to get the id of the page that is being generate with the edit form. Add this to the content template that the edit form is on [wpv-post-id] this should give you the ID of the page.

Please try this and let me know the results.

Thanks,
Shane

#1160596

Hi Shane,

For my second question;

2. I want to filter reference post by its own custom field, what I have to add in query?
What I have to change in code?

        $query->set( 'author', $current_user );

Regards,

#1161705

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Khurram,

You will need to add this.

$query->set( 'meta_query', array(
        array(
              'key' => 'wpcf-my-field',
 'value' => 'some-value',
              'compare' => '>='
        )
     
  ));

Thanks,
Shane

#1162124

Hi Shane,

For page id I tried [wpv-post-id] but this is resulting in id of current post getting edit.

#1163147

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Khurram,

So you want the query to return what exactly.

What is stored in the custom field of the reference post?

Please let me know so that I can best advise.

Thanks,
Shane

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.