Skip Navigation

[Resolved] Filter categories on post-form

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by andyB-12 4 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1862747

Hey there,

I created two different post forms where users can send in new posts. Here I they can choose from different categories, but here are shown all categories the site has. Is it possible to filter them, so the user only sees the categories of the post type this form is for?

Example:
hidden link

Here only the category "Flohmarkt" and its childs should be visible for the user.

#1862947

Hello, there's nothing built-in that will help you filter the categories in a Form field from the Form builder, but you can use a custom code snippet to restrict those terms with PHP. It's a manual process, so you would have to whitelist each individual term. The following code can be adjusted for your needs:

/**
 * Blacklist or whitelist terms in a taxonomy form field or View filter
 */
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){

    // 1. pages where form (or search View) is inserted
    $pages = array( 118, 137 );

    // 2. which taxonomy (slug)
    $taxonomy = 'status';

    // 3. add terms to blacklist *OR* whitelist, not both
    $blacklist = array();
    $whitelist = array( 'archived', 'completed' );

    if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {

        if ( !empty( $blacklist ) ) {

            foreach( $terms as $key => $term ){
     
                if ( in_array( $term->slug, $blacklist ) ) {
                    unset($terms[$key]);
                }
            }
        } elseif ( !empty( $whitelist ) ) {

            foreach( $terms as $key => $term ){
     
                if ( !in_array( $term->slug, $whitelist ) ) {
                    unset($terms[$key]);
                }
            }
        }
    }
     
    return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );

You must adjust the pages array to include a comma-separated list of the page or post IDs where this Form is shown:

$pages = array( 118, 137 );

You must adjust the taxonomy slug to match the appropriate taxonomy. For the built-in category taxonomy, you would use 'category' instead of 'status':

$taxonomy = 'status';

You must adjust the taxonomy term slugs to whitelist individual terms for the field:

$whitelist = array( 'archived', 'completed' );

Include each term in quotation marks as shown above, separated by a comma.

You can add that code in your child theme's functions.php file, or in a custom code snippet in Toolset > Settings > Custom Code.

#1867913

Hey there, thanks for the snippet.

I edited it like this:

<?php
/**
 * New custom code snippet (replace this with snippet description).
 */

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

// Put the code of your snippet below this comment.

/**
 * Blacklist or whitelist terms in a taxonomy form field or View filter
 */
function tssupp_restrict_terms( $terms, $taxonomies, $args, $term_query ){
 
    // 1. pages where form (or search View) is inserted
    $pages = array( 51281 );
 
    // 2. which taxonomy (slug)
    $taxonomy = 'category';
 
    // 3. add terms to blacklist *OR* whitelist, not both
    $blacklist = array();
    $whitelist = array( 'flohmarkt', 'antiquitaeten', 'kinder-baby', 'medien-flohmarkt', 'troedelmarkt' );
 
    if ( is_page( $pages ) && $taxonomies[0] == $taxonomy ) {
 
        if ( !empty( $blacklist ) ) {
 
            foreach( $terms as $key => $term ){
      
                if ( in_array( $term->slug, $blacklist ) ) {
                    unset($terms[$key]);
                }
            }
        } elseif ( !empty( $whitelist ) ) {
 
            foreach( $terms as $key => $term ){
      
                if ( !in_array( $term->slug, $whitelist ) ) {
                    unset($terms[$key]);
                }
            }
        }
    }
      
    return $terms;
}
add_filter( 'get_terms', 'tssupp_restrict_terms', 10, 4 );

for the form on this page: hidden link

But it does not work as intended. I added it via Toolset > Settings > Additional Code and activated it.

#1867971

I don't see anything obviously wrong in the code here, assuming the term slugs are all accurate. Is it possible there is a conflict with another similar function name? I can see another ticket in your history that discusses a similar issue: https://toolset.com/forums/topic/search-is-not-working/
If you have the same function name "tssupp_restrict_terms" used twice, there could be a PHP error preventing this snippet from running correctly. You would have to change one of the function names so both function names are unique. If that's not the issue, perhaps the custom code snippet is not activated to run Always and Everywhere?

If none of these seems to be the source of the problem I'll need to log in and take a closer look.

#1868001

I did not use any of these functions yet. But I could try and rename it, if that might help. Where do I have to rename it and how can I rename it, are there any words that have to be in the name or can the function name be individual?

Otherwise you could surely login to the backend and have a look, if thats better and faster for you. You should have the credentials from another ticket and can test it on that network site as well, as the issue needs to be fixed on all four network sites.

#1868033

But I could try and rename it, if that might help. Where do I have to rename it and how can I rename it, are there any words that have to be in the name or can the function name be individual?
You must rename the function in both the function definition and the add_filter call, as shown in the code example here. You can do this in Toolset > Settings > Custom Code in the custom code snippet editor when you edit the snippet. The function name is arbitrary, and I recommend you use lower-case letters and underscores only, like this:

...
function tssupp_restrict_terms_flohmarkt( $terms, $taxonomies, $args, $term_query ){
...
add_filter( 'get_terms', 'tssupp_restrict_terms_flohmarkt', 10, 4 );

If that doesn't solve the problem, I'll need a login. Private login credentials are automatically deleted from resolved tickets for your security, so it is probably fastest to resubmit them here rather than me try to find them in another open ticket. Private reply fields are active for you.

#1868063

Works like a charm now! Thanks a lot!

#1868065

I have another question regarding post-forms. Is it possible to not only make single line text boxes a mandatory field?

hidden link

I would like to make some of the custom fields mandatory when filling the form as well.

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/mandatory-fields-in-post-form/

#1868075

I've split that question off into a separate ticket, since it's a different topic. A supporter will follow up with you here as soon as possible: https://toolset.com/forums/topic/mandatory-fields-in-post-form/

#1878339

We just discovered another problem and the client would like, that the parent category is checked by default. Could you help me with that?
hidden link

hidden link

New threads created by Christian Cox and linked to this one are listed below:

https://toolset.com/forums/topic/preselect-specific-term-on-post-form/