Support Forum
Support › MB Relationships › Relationships + FE Submission: Trying to create relationship programmatically
I have 2 CPTs: Project
and Update
, and I need their relationship to be created programmatically at the time that Update
posts are created with a front-end form on Project
posts:
Project
posts are displayed by a dedicated CPT-specific single template: single-project.php
in the theme root.Update
posts are never displayed anywhere other than on the Project
posts to which they are related (no single display, no archive display):
register_post_type
: 'has_archive' => false
register_post_type
: 'publicly_queryable' => false
Project
post is expected to be related to one or more Update
posts *AND* every Update
post is only ever related to the single Project
post on which it was created:
add_action( 'mb_relationships_init', function() {
MB_Relationships_API::register( [
'id' => 'projects_to_updates',
'from' => [
'admin_column' => true,
'empty_message' => 'Empty "from"...',
'object_type' => 'post',
'post_type' => 'project',
],
'to' => [
'admin_column' => 'after title',
'empty_message' => 'Empty "to"...',
'object_type' => 'post',
'post_type' => 'update',
],
]);
});
Update
posts that have been manually related to Project
posts (instead of programmatically) are listed on the Project
post with:
$related_posts = new WP_Query([
'nopaging' => true,
'order' => 'ASC',
'orderby' => 'date',
'relationship' => [
'from' => get_queried_object_id(),
'id' => 'projects_to_updates',
],
]);
if ( $related_posts->have_posts() ) {
while ( $related_posts->have_posts() ) {
$related_posts->the_post();
?>
<div>
<h3><?php echo get_the_title(); ?></h3>
<div><?php echo get_the_date(); ?></div>
<div><?php echo get_the_author(); ?></div>
<div><?php echo rwmb_meta( 'update_post', $table_args_for_updates_table ); ?></div>
</div>
<?php
}
wp_reset_postdata();
}
Update
posts are created on the front end with a form that appears on every Project
post:
single-project.php
: echo do_shortcode( '[mb_frontend_form ajax="true" id="update-fields-group" post_fields="title,update_post"]' );
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( get_queried_object_id(), $post_id, 'projects_to_updates', $order_from = 1, $order_to = 1 );
}, 10, 2 );
The problem is that while Update
posts are indeed created, there is never a relationship created between these new Update
posts and the Project
post on which they were created. If I relate them manually, they display on the Project
post as expected.
So, I know I'm almost there with this first round of requirements, but even after reviewing multiple threads here, I cannot get the relationship to be created at Update
post creation.
Hello,
I think you can try a few things below:
2b. Change 'publicly_queryable' => true
5b. Change the shortcode to this
[mb_frontend_form ajax="true" id="update-fields-group" post_fields="title" post_type="update"]
If it still does not work, please add some existing IDs to the code and see if the relationship is created
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( 123, 456, 'projects_to_updates' );
}, 10, 2 );
where 123 is a project
post ID, 456 is a update
post ID.
Thank you for the assistance!
So far, no joy: Relationship not created.
What do you find are the most common errors in configuring this sort of functionality? Is it possible I'm missing a component? Making calls in the wrong order? Maybe some other CPT config that is known to interfere with this functionality?
Hello,
If this code
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
MB_Relationships_API::add( 123, 456, 'projects_to_updates' );
}, 10, 2 );
still does not work, please try to deactivate all plugins except Meta Box, MB extension plugins and switch to a standard theme of WordPress then check this issue again.
I will certainly do that if there is no other choice, but given that there might be quite a bit to re-implement in a new [child] theme on a new server (all the work thus far is implemented in an Astra child theme), I'd like to try to confirm a couple of things with you:
add_action
for mb_relationships_init
in my original post, plus...publicly_queryable
setting change you specified, plus...mb_frontend_form
shortcode change you specified, and...add_action
for rwmb_frontend_after_process
you outlined with fixed ID's......already accounts for all that is required for the specified relationship to be created upon submission via front-end form? There is nothing else you can suggest checking before reimplementing with a default theme?
Apologies for asking some of this stuff again. I'm just trying to avoid reimplementing this on a new Stager, as I cannot deactivate everything in Production (or even on the current Stager). I'm happy to do all of this, of course, but I'm also hoping that someone's memory or alternative ideas might be jogged by one more round of questions...
I just noticed something that might be helpful: When echoing get_queried_object_id()
and $post_id
:
get_queried_object_id()
returns correct ID of the project
post on which the MB Frontend Submissions form is displayed (and used to create update
posts)$post_id
return nothingThat's strange. If an "update" post is created, the variable $post_id
should be the post ID. Please share your staging site admin account via this contact form https://metabox.io/contact/
I will help you to check the issue.