Skip Navigation

[Resolved] wpt_field_options filter

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 16 replies, has 2 voices.

Last updated by svenA-3 1 year ago.

Assisted by: Minesh.

Author
Posts
#2658347

Tell us what you are trying to do?
Populate a select field for a custom post type dynamically

Is there any documentation that you are following?
Yes: Is the documentation https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options still up to date? It carries a warning heads-up about future types releases. And it does not work for me currently.

current code:

function g5yearscheck() {
$syear = '2022';
$cyear = date( 'Y', time() );
$year = $syear;
$match = array();

while ($year <= $cyear) {
// Query arguments
$args = array(
'post_type' => 'leistungsreport',
'post_status' => 'private',
'post_title' => $year
);

// Create a new WP_Query instance
$query = new WP_Query( $args );

// Check if the post exists
if ( $query -> have_posts() ) {
// The post exists
$match[] = $year;
error_log( print_r( $match, true ) );
$year++;
continue;
}

$year++;
}
wp_reset_postdata();
return $match;
}

function g5populateyearselect( $current_options, $title_of_field ) {
// each internal annual report post has its year as post title
// so there must be only one editable post per year
// the year selection is to start from 2022 until current year
// form ID: 48004

error_log( 'year select filter start' );
error_log( print_r( $current_options, true ) );

// make sure we have the intended field
if ( $title_of_field != 'Berichtsjahr' ) {
return $current_options;
}

$match = g5yearscheck();
error_log( print_r( $match, true ) );
if ( !empty( $match ) ) {
$new_options[ 0 ] = $current_options[ 0 ];

foreach( $match as $m ) {
$new_options[] = array(
'#value' => $m,
'#title' => intval( $m )
);
}
error_log( print_r( $new_options, true ) );
}
else{
error_log( 'no options' );
}

return $new_options;
}
add_filter( 'wpt_field_options', 'g5populateyearselect', 10, 2 );

#2658411

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Yes - the filter is still supported and there is no change in it.

I need to dig into your code and check why its not working. Can you please tell me what you want to display with your dropdown options and share problem URL where I can see the dropdown and send me admin access details.

Please share details where you added the code you shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2658629

Oh yes: the frontend page in question: hidden link (need to be logged in as admin)

#2658717

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see with the code snippet you shared to look at it has too much code added to it with comment and its not easy to look at it.

Can you please give me another code snippet where I will have to look only code for the dropdown.

Also, with the problem URL you shared:
- hidden link

I see PHP information page, can you please tell me where you added the custom select field for which you are trying to fill the select options?

#2658937

Minesh,

hidden link is not the problem URL and I have clearly marked it, quote: "for php configuration", in case environment might be important.

I have sent another reply that included the problem URL 3 minutes after the private reply. Haven't you received it?

The isolated code of relevance has already been included in the opening text of this ticket. It includes 2 functions. Please use it as reference if the complete code has too much content for a quick analysis. That's why I've presented the isolated code in the first place.

Regards,

Sven

#2659103

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see your code and it does return the one matching record and also that returned record by query when you loop through it, it does display the one option as 2022.

It seems to be ok and there does not seems to be any issue for now. Do you have any other queries related to this?

#2659283

Well, 2023 is missing. The loop of the filter ends with the current year and a matching post titled 2023 exists. But it isn't added to the dropdown.

Sven

#2659427

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see now on your page you have a view that displays the dropdown that shows 2022 as only option.

I see you have added the following form as well to your page:
- hidden link

I see you applied conditional display with that and wrapped your field "Berichtsjahr" with conditional display. I see that is hidden due to conditional display you added and that does show both 2022 and 2023 as option.

Please note that you can update the options for custom field display not for the view's frontend filter. On the top of the page that displays the only 2022 option is a view's frontend filter and you can not update the options for that using "wpt_field_options" hook.

I see you already added the query filter for the following view that displays the frontend filter:
- hidden link

You added the frontend filter as follows which displays the only option 2022:

[wpv-filter-controls]
<div class="form-group">
	<label for="wpv-wpcf-berichtsjahr">[wpml-string context="wpv-views"]Berichtsjahr[/wpml-string]</label>
	[wpv-control-postmeta type="select" field="wpcf-berichtsjahr" name='Berichtsjahr' order="desc" url_param="wpv-wpcf-berichtsjahr"]
</div>[wpv-filter-submit output="bootstrap" class="btn-secondary"]
[/wpv-filter-controls]

The query filter added for the view is:

  Custom field filters Edit
Select elements with field:
Filter based on the frontend search filter by reporting year .

Do you want to update the options for the frontend filter select box that is available with the view on top of the page? if yes:

The strange thing I noticed is that the filter for the field "Berichtsjahr" is the to filter by URL param but as soon as I save the filter it changes automatically to the following:

Filter für benutzerdefinierte Felder Editieren
Elemente mit Feld auswählen:
Filter based on the frontend search filter by Berichtsjahr.

I tried to check how you added the view but it shows that you are editing the page:
- hidden link

#2659497

Oh, I simply left that page open and forgot to close it. It's an Elementor page and the view is added via Elementor widget.

#2659499

"Filter für benutzerdefinierte Felder Editieren
Elemente mit Feld auswählen:
Filter based on the frontend search filter by Berichtsjahr."

This change seems to be kind of an automatic, but incomplete translation to German.

Sven

#2659501

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You did not clarify yet - do you want to display 2022 and 2023 posts with the view's filter dropdown added at top of the page?

As "wpt_field_options" works only to update the custom field select options not the view's filter select options.

#2659529

I need a solution for populating the search filter of the view so only the result of a single year be displayed. If not possible with wpt_field_options then I need an alternative here.

#2659555

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

On this page I can see two forms for now to edit information:
- hidden link

Do you mean that you want to display one form at a time, having said that, when the above page loads it should only display the select box with two options 2022 and 2023 and when user select 2022 it should display the form belongs to 2022 and when user select 2023 from the filter dropdown it should display the form belongs to 2023. Is that correct?

#2659621

when the above page loads it should only display the select box with all options 2022 to current year (this year: 2022, 2023; next year 2022, 2023, 2024 and so on) and when user selects 2022 he should see the leistungsreport post named 2022 as the result.
Currently he can't select 2023 though, because it ain't there.

#2660005

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

In that case the easiest way would be to add the dropdown select filter with manually set values as given under for your view:
=> hidden link

<div class="form-group">
	<label for="wpv-wpcf-berichtsjahr">[wpml-string context="wpv-views"]Berichtsjahr[/wpml-string]</label>
	[wpv-control-postmeta field="wpcf-berichtsjahr" type="select" source="custom" url_param="filter-year" values=",2022,2023,2024,2025,2026,2027" display_values="Please Select,2022,2023,2024,2025,2026,2027"]
</div>

As you can see I've setup the filter with:

values=",2022,2023,2024,2025,2026,2027" display_values="Please Select,2022,2023,2024,2025,2026,2027"

Now if you can filter the result it will display the selected option form:
- hidden link