Skip Navigation

[Resolved] Predefined parent value to menu lin

This support ticket is created 3 years, 9 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
- 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 10 replies, has 2 voices.

Last updated by katjaL 3 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#1973123

If I understand right, you can create child posts in cred forms so that the user 1. chooses from all the parents, 2. chooses from own custom post parents, 3. the parent is chosen ready in select (defined by page where user is or where she/he comes from).

Now I have child posts which can't be created in parent post page (too much info on one page). Also one has to be able to create a child post from menu link, not be forced to go to parent first and then to child form page. So I'm trying to create that menu link, which somehow can carry the parent information within. Or how should I approach this?

Anyway what I need is a child post form page which has the parent post already defined (and hidden) and this page should be reachable from menu. BTW the parent posts are restricted, the current author can create only one parent, so what would work also: predefined would be current logged in author's parent post.

#1973385

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

If you know the parent ID (as you acknowledge that there will be only one post available as parent post), based on the current author we can query the post type to find out the post belongs to current author and then we can connect that found post ID and set it as parent to the child post.

For that, we will require to use the Toolset form's hook: cred_save_data
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

And to connect the posts, we can use the Tooslet post relationship API hook: toolset_connect_post
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_connect_posts

Please check the following related ticket (its not exactly the same as you will require to get the parent post ID by querying the current author and you will require to adjust the code accordingly)
- https://toolset.com/forums/topic/i-need-to-automatically-set-the-related-post-directly/#post-1082747

#1979267

Thank you Minesh. I was wondering if it was possible to add the url parameter of parent into the menu link. The menu link is normally like this hidden link
and it should be hidden link

So the XXX part should come dynamically as shortcode. Do you think this could be done?

#1979497

Minesh
Supporter

Languages: English (English )

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

I wonder why you want to add the parent ID as URL param if we able to know the parent but if its not known yes, you will require to pass it as URL param.

If you can explain me the flow where exactly you want to navigate from where to where and where exactly you want to add URL param then I can guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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.

#1980497

Minesh
Supporter

Languages: English (English )

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

I've added the following code to "Custom Code" section offered by Toolset:

function func_add_parent_as_url_param_wp_menu(){
    if( is_user_logged_in() ){
        add_filter( 'nav_menu_link_attributes', function( $atts, $item ){
            if( 'Uusi teos' == $item->title ){
                $current_user = wp_get_current_user();
                $args = array(
                  	'post_type'=>'taiteilija',
  					'author'        =>  $current_user->ID, 
  					'posts_per_page' => -1, // no limit
                    'fields'=>'ids'
					);
					$current_user_posts = get_posts( $args );
              
                $atts['href'] = esc_url(
                    add_query_arg(
                        array( 
                            'parent_taiteilija_id' => $current_user_posts[0] ,
                           
                        ),
                        $atts['href']
                    )
                );
            }
            return $atts;
        }, 10, 2 );
    }
}
add_action( 'template_redirect', 'func_add_parent_as_url_param_wp_menu' );

The above code will find the parent post ID based on the current author and add the parent ID as url param.

Now, once you login as user "testing" and click on the link Oma profiili->Uusi teos, you will see the parent ID set with URL param and the parent is selected automatically.

Can you please confirm it works at your end as well.

#1980513

Wow, perfect! My issue is resolved now. Thank you Minesh so much!
EDIT: I try to reopen this, because the code doesn't work as expected at the real site as it does in clone site. Now it seems the reopening here doesn't work 🙂
That is why I edit this closing.

#1980517

Hi Minesh, sorry to open this again. Now that I insert the code into the real site, for some reason it doesn't work. I can't figure out, what must be changed. I added the code to the Toolset Settings Custom code as in clone site and checked the form and the menu are the same. Did I miss something?

#1980525

Hi Minesh, sorry but I can't make it work in the real site as in clone site. I have copied the code to Settings-Custom code and checked the form + menu, they are the same. What am I missing?

#1980527

I try to reopen this now for the third time, the code doesn't work as expected at the real site.

#1980529

Minesh
Supporter

Languages: English (English )

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

Can you please make sure that you activated the code snippet you added to "Custom Code"

As well as the menu names are correct. If you can send me access details to your live site with access details of test user I'm happy to look whats going wrong.

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

#1980531

I'm so sorry for spamming the forum with reopening, you can delete those messages. For some reason they didn't show up. Anyway I haven't added the custom code before myself, so I didn't realize there was this activate feature. Everything works perfectly now! My issue is resolved now. Thank you!