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?
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.
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.
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.
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.
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...
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"?
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.