how to preselect category in “add post” form?

[ad_1]

Hello @hugofant,

I hope you’re doing great today!

Please try using this PHP snippet: https://gist.github.com/wpmudev-sls/df4dc9c4bf21fc17fd37bdebb02a4db8

– save it as a PHP file, for example: forminator-set-default-category-in-postdata.php, and upload it to /wp-content/mu-plugins/ directory on the server, so that it runs as a must use plugin.

After that you can set the default category on the ADVANCED tab of the field options:
https://share.getcloudapp.com/KouZ0GNy

Hope this helps. Please let us know if you have any questions.

Best Regards,
Dmytro

Hi Dmytro,
thanks for your answer.
The snippet works, but not as expected. When selecting a value in the ADVANCED tab, the field is no longer visible on the form afterwards….as written, i want to do only a preselect, but the user should be able to overwrite the selection if its not OK for him.
best regards
Hugofant

Hi @hugofant

Thanks for response!

I checked and you’re right – code hides the category choice entirely. There’s an option to set it to not hide it but it would still block users from selecting category other than default.

We need to consult it with our developers then so I’ve asked them to take a look into it. I’d appreciate some patience as they are dealing with a lot of complex tasks on daily basis and their response time may be slightly longer than ours but we’ll update you here as soon as we got solution form them.

Please keep an eye on this space for further information soon.

Best regards,
Adam

Hi @hugofant

I just got an update form our developers and they came up with a solution.

Please replace entire previously shared code with this one:

<?php 

add_filter( 'forminator_field_postdata_category_list', function( $categories ) {
	$deafult_cat = 35; // Please use the default category ID.
	foreach ( $categories as $id => $cat ) {
		if ( $cat->term_id === $deafult_cat ) {
			$item = $categories[ $id ];
			unset( $categories[ $id ] );
			array_unshift( $categories, $item );
			break;
		}
	}
	return $categories;
});

Then you need to set default category directly in the code (there won’t be option to select in field settings) and to do this:

1. go to categories list (Posts -> Categories) in site’s back-end, find the category you want and check it’s numberical ID;

to check that ID, the simplest way is to click “Edit” for given category and look into the URL, the category ID will be the number after the “tag_ID=” part of the URL

2. so use that number instead of number 35 in this line of code

$deafult_cat = 35; // Please use the default category ID.

Save the file and that’s it. From now on this category will be preselected but the category drop-down list will still be visible and remain active so users will be able to select different category.

Best regards,
Adam

Hi Adam,
thanks for that superfast solution. works like a charm.

best regards
Hugofant

Hi @hugofant

I’m glad I could help!

I’ll mark this as resolved then but in case you’d have any other questions, don’t hesitate to tart new topics and we’ll be happy to assist.

Best regards,
Adam

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer