Skip Navigation

[Resolved] WordPress hierarchy in custom posts

This support ticket is created 5 years, 7 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 75 replies, has 2 voices.

Last updated by Shane 5 years, 5 months ago.

Assisted by: Shane.

Author
Posts
#1235069

Dear Sirs

I have an issue when using the built in WordPress hierarchy with CPT's of the same kind.

I have a CPT 'Person' and the desired outcome is to add another CPT 'Person' as their parent. So Person 2 becomes Person 1's Parent. I have tried to achieve this using the following pages and code:

PAGE 1 - This displays a list of PEOPLE with a link next to each PERSON that takes you to the LAYOUT for that PERSON
PAGE 2 - On this layout page for the PERSON selected (lets say PERSON 1) there is another list of potential Parents (Other CPT PEOPLE - Lets say PERSON 2) - with a link next to them taking you to the Edit form page for PERSON 1 but also sending the ID of the potential parent(PERSON 2) in the URL (parent_id=CPT ID of PERSON 2).
PAGE 3 - This is the edit page for PERSON 1 and I have added the line
[cred_field field="post_parent" class="form-control" output="bootstrap" value="[wpv-search-term param='parent_id']"]
so that the PERSON 1 CPT picks up the ID and adds PERSON 2 as their parent.

The URL Structure for the 3 pages is as follows:
PAGE 1 - hidden link
PAGE 2 - hidden link
PAGE 3 - hidden link

THE PROBLEM!
When the edit form is submitted on page 3, PERSON 2 is indeed added correctly as the parent to PERSON 1, HOWEVER a new CPT PERSON is created also (Lets say PERSON 3) and is automatically assigned as the child of PERSON 2 so there is a three way relationship set up. Then when you go back to Page 1 to repeat the process the url structure changes to:
PAGE 1 - hidden link
PAGE 2 - hidden link
PAGE 3 - hidden link

Thus creating yet another CPT PERSON (Lets say PERSON 4) and so on and so-forth. Also when you add more 'potential parents' to Page 2 it starts to add the parent to the wrong child and gets all mixed up.

Please help

Best regards

Geoff

#1235115

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

I've moved tho to a forum ticket.

Could you provide me with the access tot the site to check on this for you.

Thanks,
Shane

#1235291

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

Could you send me a link to the pages with the form?

I filled out the form here hidden link

However the will is created only once on the backend.

So i need further information on the replication steps.

Thanks,
Shane

#1235304

Hi Shane
Thanks for getting back to me...

I have created one child (Child 1) and one person (Parent 1) that I want to become the post parent.. Both Child and Parent are the same CPT (PERSON) If you go to:
hidden link
This is PAGE 1 and displays your CHILD 1
If you click on Choose/Change Parent it will take you to the layout for PERSON (i.e. CHILD 1) :
hidden link
This is PAGE 2 If you select next to the 1st Parent 1 in the list 'Choose As Guardian' - This takes you to the form (PAGE 3) that adds the parent to the child record and also the error of the extra record when 'Add As Guardian' is clicked

Thank you for looking at this...I shall look forward to hearing back from you.

Regards
Geoff

#1235556

Hi Shane...just a bit of information to add..I mentioned earlier that the site is automatically creating new pages when I don't want it to...This could be part of the same problem... and example is if you navigate to page:
hidden link
and click 'Submit' - this automatically creates a new page...

The other issue which again may be connected to the problem is that when I edit a form from within a view it doesn't save it - I have to go to the form directly..

There's definitely something not fully working....so to summarise the issues:

1. Repeating Parent Issue Above
2. Creation of unwanted new Pages
3. Unable to edit forms from within a view

Thank you for looking at this
Best regards

Geoff

#1235893

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

I tested the form below
hidden link
It actually doesn't create a new post since it is an edit form. If the form is creating a new post please let me know where it is stored because I submitted the form and checked the User will CPT and no new posts were added

For the issue with the Form in a view, this is something that we are aware of and working on to resolve.
https://toolset.com/errata/inconsistent-behavior-when-submitting-an-editing-form-in-a-view-listing-posts/

If you are using AJAX submit on the form then it won't work.

Thanks,
Shane

#1235910

Hi Shane

Thanks for responding. It looks like you haven’t addressed my first issue of the parent/child issue... this is in some ways the most critical....

In regards to the other points you have addressed:
1. hidden link
This edit form creates a new PAGE and not a POST... the page created is called About Yourself Introduction

2. Editing from within VIEWS - Ok that’s fine as long as it’s being addressed...

I shall look forward to your response to the initial enquiry about the parent / child issue

Best regards
Geoff

#1236411

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

It can be a little confusing handling multiple things at the same time.

So lets try to catch our bearings a bit. You're saying this page here hidden link

Once the user clicks on the "Begin About Myself" it creates a new page? This i'm not seeing.

It leads me to this page here
hidden link

Submitting this form doesn't create a new page

This doesn't happen on my end. Are you able to do a little short video highlighting with happens on your end ?

Thanks,
Shane

#1236425
Screen Shot 2019-04-26 at 20.36.57.png
Screen Shot 2019-04-26 at 20.36.21.png

Hi Shane...apologies..I agree let's deal with things one at a time. The three main issues are:

1. Wrongly creating pages.
2. WordPress hierarchy issues with CPT's of the same kind.
3. Not able to edit forms in a view

Let's look at number 1 first -
I have an issue that when I submit several of my CRED forms they automatically generate a new page with the same name as the page the form is on ...so for example if I have a page called url.com/page-one with a CRED form on, when I submit it creates a new page in the database called page-one-2 and so on and so forth.

An example of this is on page:
hidden link
When the form is submitted by clicking 'Begin About Myself' it correctly redirects you to the page hidden link but also wrongly adds a new page to the database called about-yourself-2 or 3/4/5 etc. I have only created around 40 pages but the database has over one thousand created. I have attached 2 screen shots of the pages page... if you look at the numbers(All/Published/Drafts/Bin) you'll see the auto increment before and after I submit the form...

Hope this makes sense
Best regards

Geoff

#1236446

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

Great so right now we are dealing with
1. Wrongly creating pages.

In my testing I was finally able to observe this though I'm not able to find the page.

I checked your functions.php file and I would recommend that you start fresh by removing all of your custom hooks and then re-add them one by one.

I suspect there is a cred_save_data hook that is causing this, however I would still recommend removing all of them since there are so many, its difficult to tell which one is causing the issue.

Thanks,
Shane

#1236541

Good morning Shane

Ok now I think we are making progress...I took yoiur advice and removed all of the custom hooks and added them back one at a time.. I have now isolated the hook that is creating the issue with automatically adding pages. I think that this is also causing the problem with issue:
2. WordPress hierarchy issues with CPT's of the same kind

Here is the hook:

/* Function to make new person a parent of the referred child ID */
add_action('cred_save_data','func_update_parent_field',10,2);function func_update_parent_field($post_id,$form_data) {    		if ($form_data['id']==2613 or 2780) {       				wp_update_post(array('ID' => $_REQUEST['child_post_id'],         'post_parent' => $post_id    ));    				update_post_meta($_REQUEST['child_post_id'],'wpcf-adopted',$_POST['wpcf-adopted']);		}}
/* END - Function to make new person a parent of the referred child ID */

The purpose of this code is that when a new CPT 'PERSON' is created using either forms 2613 or 2780 that it will take the child_post_id from the url and make the new PERSON a parent of the passed child. It also should add the value 'Yes' to the 'Adopted' field

This is code that Minesh wrote when I needed previous support... Can you suggest why it is creating the new pages and how it can be fixed...

Happy weekend!

Best regards

Geoff

#1237346

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

I believe the issue is in the conditional code to get the hook to fire.


add_action('cred_save_data','func_update_parent_field',10,2);
function func_update_parent_field($post_id,$form_data) {          
      if ($form_data['id']==2613or 2780 ) {                       
           wp_update_post(array('ID' => $_REQUEST['child_post_id'],         'post_parent' => $post_id    ));                 
           update_post_meta($_REQUEST['child_post_id'],'wpcf-adopted',$_POST['wpcf-adopted']);    
     }
}

First I would like you to try it with just the 2613 id form.

Then if that works fine and there are no duplicates then try this now.


add_action('cred_save_data','func_update_parent_field',10,2);
function func_update_parent_field($post_id,$form_data) {          
      if ($form_data['id'] == 2613 or $form_data['id'] == 2780 ) {                       
           wp_update_post(array('ID' => $_REQUEST['child_post_id'],         'post_parent' => $post_id    ));                 
           update_post_meta($_REQUEST['child_post_id'],'wpcf-adopted',$_POST['wpcf-adopted']);    
     }
}

Please let me know if this helps.
Thanks,
Shane

#1238810

Hi Shane

Hope you're ok...

I have tried the first code with only one form data reference

if ($form_data['id']==2613 ) 

And that works fine, there is no new page creation and also the parent is created and associated with the child that is passed via the url and also the adopted field is correctly changed to 'Yes'

However...

When I use the second code you suggested

if ($form_data['id'] == 2613 or $form_data['id'] == 2780 )

Then there is no new page creation which is good and the new PERSON gets created which is also good but the hook doesn't work i.e the PERSON doesn't become the parent of the child passed in the url and also The Adopted field doesn't become 'Yes'

What do you think?

Best regards

Geoff

#1238824

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Geoff,

This or statement should work.

However there is another way to do it.


if ($form_data['id'] == 2613 || $form_data['id'] == 2780 )

This one should work.

Thanks,
Shane

#1239265

Hi Shane

Now it doesn't even work with the original code....help! I'm stuck!

I'm using:

add_action('cred_save_data','func_update_parent_field',10,2);
function func_update_parent_field($post_id,$form_data) {          
      if ($form_data['id'] == 1787 ) {                       
           wp_update_post(array('ID' => $_REQUEST['child_post_id'],         'post_parent' => $post_id    ));                 
           update_post_meta($_REQUEST['child_post_id'],'wpcf-adopted',$_POST['wpcf-adopted']);    
     }
}

Where form 1787 is the creation of new CPT 'Person' and this person is the parent post of the child passed in the url...

What happens in that the new Person gets created but doesn't get made the parent of the passed child ID and also the child custom field 'Adopted' doesn't get filled with a 'Yes'..

This function was working fine apart from the duplicate pages ..now it doesn't work at all

Best regards

Geoff