Relationships + FE Submission: Trying to create relationship programmatically
Support › MB Relationships › Relationships + FE Submission: Trying to create relationship programmatically
- This topic has 6 replies, 2 voices, and was last updated 1 year, 9 months ago by
Peter.
-
AuthorPosts
-
July 14, 2023 at 9:22 AM #42561
Digital Rockery
ParticipantI have 2 CPTs:
Project
andUpdate
, and I need their relationship to be created programmatically at the time thatUpdate
posts are created with a front-end form onProject
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 theProject
posts to which they are related (no single display, no archive display):- In
register_post_type
:'has_archive' => false
- In
register_post_type
:'publicly_queryable' => false
- In
- Every
Project
post is expected to be related to one or moreUpdate
posts *AND* everyUpdate
post is only ever related to the singleProject
post on which it was created:- MB Relationships installed
-
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', ], ]); });
- Existing
Update
posts that have been manually related toProject
posts (instead of programmatically) are listed on theProject
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(); }
- New
Update
posts are created on the front end with a form that appears on everyProject
post:- MB Frontend Submission installed
- In
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 newUpdate
posts and theProject
post on which they were created. If I relate them manually, they display on theProject
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.July 14, 2023 at 7:47 PM #42569Peter
ModeratorHello,
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 aupdate
post ID.July 15, 2023 at 12:00 AM #42576Digital Rockery
ParticipantThank 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?
July 15, 2023 at 3:45 PM #42584Peter
ModeratorHello,
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.
July 16, 2023 at 9:47 AM #42593Digital Rockery
ParticipantI 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:
- Is it indeed the case that my config as currently implemented...
- MetaBox, MB Frontend Submission, and MB Relationships, plus...
add_action
formb_relationships_init
in my original post, plus...publicly_queryable
setting change you specified, plus...mb_frontend_form
shortcode change you specified, and...add_action
forrwmb_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?
- Is there a demo somewhere (or walkthrough article/tutorial) that I can look at?
- Are there any test or profiling tools I can install in Staging (or Dev) to provide you with more info (I already have Query Monitor running)?
- Assuming for a moment that the same problem persists even in a reimplemented version, would you be able to have someone log in to the new server and take a look?
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...
July 16, 2023 at 1:48 PM #42595Digital Rockery
ParticipantI just noticed something that might be helpful: When echoing
get_queried_object_id()
and$post_id
:get_queried_object_id()
returns correct ID of theproject
post on which the MB Frontend Submissions form is displayed (and used to createupdate
posts)$post_id
return nothing- This behavior is the same whether the test echos are placed directly after the shortcode or after the MB Frontend Submissions form
July 17, 2023 at 10:42 PM #42609Peter
ModeratorThat'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. -
AuthorPosts
- You must be logged in to reply to this topic.