Skip Navigation

[Resolved] Standard WP fields not available in filter field list

This support ticket is created 4 years, 2 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 4 replies, has 2 voices.

Last updated by nicolaS-3 4 years, 2 months ago.

Assisted by: Nigel.

Author
Posts
#1526751

Hi,
I have two CPTs, AAA and BBB, related with a one-to-many relationship. AAA is parent. I am creating a custom search view where I want the user to select one BBB and show AAA data.
I would like the user to select the BBB ID or title, but these fields are not available in the filter field list because they are standard WP fields. Why are they not listed ?

I tried to force the field in the filter search code like this:
<div class="form-group">
<label for="wpv-wpcf-post-title">[wpml-string context="wpv-views"]Please select one BBB:[/wpml-string]</label>
[wpv-control-postmeta field="wpcf-post-id" type="select" url_param="wpv-wpcf-post-id"]
</div>
but the select on the page is empty and the view displays all BBBs ...

What am I doing wrong ?
Thanks
Regards
Nicola

#1526827

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Nicola

Views custom search only supports filtering by custom fields or taxonomies, filtering by standard fields such as post title is not supported.

There is an old request for this but it is not being actively developed, you can vote for it here: https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

The simplest alternative is to use a text search field that searches just post titles.

A more complex alternative would involve generating your own filter control for the front-end in the form of a select dropdown that listed the post titles—and a separate one that listed the post IDs if necessary—which added the post ID as a URL parameter.

You could then add a Query Filter for the post ID that listened to that URL parameter.

If you want to try that I can try and guide you, though I can't be sure it will work, we would effectively be hacking an unsupported feature.

#1526947

Hi Nigel,
thanks for your reply. I might have another workaround. When I create a new BBB record with a specific form and page, I have added a PHP function to set post title = post ID (as suggested by Christian) and it works perfectly. So I have added a new custom field (slug=nr-bbb) and I can use the same function to set it as the ID so that I can use this field for the filter select. The function is the following:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==12345)

{
$my_post = array(
'ID' => $post_id,
'post_title' => $post_id,
'nr-bbb' => $post_id
);

// Update the post into the database
wp_update_post( $my_post );
}
}
Although I used the field slug in the PHP function it doesn't work ... the new BBB post is created, but the nr-bbb field is empty. Do I need a special syntax to do this ?? any clue ? thanks
Regards
Nicola

#1527141

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

You are trying to add a custom field (post meta) as if it were a standard post field, using wp_update_post.

You add custom fields using add_post_meta (https://developer.wordpress.org/reference/functions/add_post_meta/), or try update_post_meta (https://developer.wordpress.org/reference/functions/update_post_meta/) if you will use the same with an edit form.

#1527201

Thanks Nigel, my issue is resolved now.
I used

 add_post_meta($post_id, 'wpcf-nr-bbb', $post_id, false);

It works !
Thank you!
Best regards
Nicola

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