Skip Navigation

[Resolved] Post Relationship Cancells when post not published

This support ticket is created 7 years, 8 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by davidG-5 7 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#425777

Hi.

I have a custome post type call business-listing and then a child post called accreditations.

Why does the plugin remove the post relationship if the parent is not published? Is there a way to work around this to ensure that the relationships remains in tact regardless of the parent's post status?

#425778

It also appears to remove the relationship when editing the child post using a CRED, edit form. Why is this?

#425820

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Can I get some more info from you about what you are seeing happening where.

When you said the post relationship is removed if the parent is not published, what exactly do you mean.

If you have used Types to set up parent-child relationships between your post types and created a parent post that only has a status of draft, for example, you can still add that post as a parent when you create or edit a post of the child post type.

If you had one of your parent post type posts as published, connected some child posts, then changed the status of the parent post to pending, you would still see all of the connections with the child posts.

Can you clarify what you mean and then I might have a better answer for you.

#425828
screenshot-4.jpg
screenshot-3.jpg
screenshot-2.jpg
screenshot-1.jpg

Hi Nigel.

My apologies (it made sense in my head)

Ok, so I basically what is happening is that if I have a parent -> child relationship and the parent post is not published, then when editing the child with CRED the relationship is removed.

Please see attached screenshots showing this.

So if I have a listing called "Local Test" and it is published. I can then create a child post "Accreditation" and assign Local Test as the parent. I cannot set this relationship unless "Local Test" is published.

If I have both "Local Test" and "Accreditation" published and the relationship is set, If for some reason make "Local Test" draft/pending and then edit the published "Accreditation", it removes the relationship and can no longer be assigned to "Local Test" until it is published.

When I say removes the relationship, it shows this both in the backend of the post and in the database as my code is referencing the DB directly and fails to find the relationship after editing "Accreditation" and "Local Test" is not published.

#426036

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

I did some testing with CRED and was able to reproduce the issue.

There is no problem making the post connections in the back-end if the status is not published, but it is not possible on the front end via CRED forms.

I discussed with my colleagues whether this was a bug or whether it was by design and the current consensus is that the behaviour is intended. Posts which have a draft or pending status are hidden from front-end users by design. I've suggested that it might depend on the user capabilities, so that an Editor or Admin would see the parent posts regardless of status, for example.

In the meantime, you can try the following workaround.

There is a block of HTML to add to your CRED form in place of the current parent-post selector block. Then some JS to add to the custom JavaScript section of the form. Then some PHP to add to your functions.php file which creates a custom shortcode to generate the parent post ids, regardless of post status.

<div class="cred-group cred-group-parents">
  <div class="cred-field cred-field-_wpcf_belongs_listing_id"> // change "listing" to your parent post slug
    <label class="cred-label">
      Choose Parent
    </label>
    <input type="hidden" id="parents_id" value="[get-parents]" />
      [cred_field field='_wpcf_belongs_listing_id' value=''] // change "listing" to your Parent Post Slug
  </div>
</div>
jQuery('document').ready(function(){
   
var post_parents = jQuery('#parents_id').val();
var arr = post_parents.split(',');
     
jQuery("[name=_wpcf_belongs_listing_id] > option").each(function() { //change "listing" to your parent post slug
       
    var option_val = jQuery(this).val();
       
     if( jQuery.inArray(option_val, arr) == -1 && option_val != -1 ){
       jQuery(this).remove();
     }
  }); 
});
function get_parents($atts) {

    $parent_args = array('post_type' => 'listing', 'posts_per_page' => '-1','post_status' => 'any',); // change "listing" to parent post slug
    $parent_posts = new WP_Query($parent_args);
    $parent_ids = "";
    while($parent_posts->have_posts()) : $parent_posts->the_post();
        $parent_ids .= get_the_ID() .",";
    endwhile;
   
    return $parent_ids;
}
add_shortcode('get-parents', 'get_parents');

Note that I have assumed your parent post type slug is 'listing' but you will need to change the code in several places if different.

#427450
Screen Shot 2016-08-24 at 12.14.04 PM.png
Screen Shot 2016-08-24 at 12.31.44 PM.png

Hi Nigel.

Thank you for your assistance. Unfortunately it still does not work. I have the parentID saved in a different DB table and even when I set that as the value in the CRED belongs to field it does not find the listing (parent post).

It appears that CRED won't show the parent posts even if we manually set the ID. Your code didn't work in terms of the dropdown as it did not populate draft/pending posts but only published. The Javascript however does work provided that the parent post is published.

Is there perhaps another workaround whereby we don't set the parent through the frontend editor but rather insert it into the DB via the cred_save_complete api call? https://toolset.com/documentation/user-guides/cred-api/#csc

In the screenshots below you can see that the parentID is found but because the status is not published then it won't find it in the dropdown. But then if I publish the list it works...

Thanks

#427634

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi David

Sorry, can I just check what you mean by having the parent post ID saved in a different database table. You have done something to alter the default behaviour that it is stored as wp_postmeta with the key "_wpcg_belongs_parent-slug_id"?

#427874

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi David

A bit more input from colleagues.

There is an undocumented filter available for this.

The solution may be as simple as:

add_filter('cred_get_potential_parents_post_status', 'potential_parents_post_status');
function potential_parents_post_status( $statuses )
{
    return 'any';
}

If you want to try that and let me know how you get on.

#427918

Hi Nigel.

We have the relationship stores in a different table we created to record additional information needed. It in no way alters the post_meta or the behavior of the plugins.

The filter you provided me with works. Thanks you very much!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.