Relationships + FE Submission: Trying to create relationship programmatically

Support MB Relationships Relationships + FE Submission: Trying to create relationship programmatically

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #42561
    Digital RockeryDigital Rockery
    Participant

    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:

    1. Project posts are displayed by a dedicated CPT-specific single template: single-project.php in the theme root.
    2. Update posts are never displayed anywhere other than on the Project 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
    3. Every 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:
      • 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',
                ],
            ]);
        });
        
    4. Existing 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();
      
      }
      
    5. New Update posts are created on the front end with a form that appears on every Project 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 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.

    #42569
    PeterPeter
    Moderator

    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.

    #42576
    Digital RockeryDigital Rockery
    Participant

    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?

    #42584
    PeterPeter
    Moderator

    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.

    #42593
    Digital RockeryDigital Rockery
    Participant

    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:

    1. Is it indeed the case that my config as currently implemented...
      1. MetaBox, MB Frontend Submission, and MB Relationships, plus...
      2. add_action for mb_relationships_init in my original post, plus...
      3. publicly_queryable setting change you specified, plus...
      4. mb_frontend_form shortcode change you specified, and...
      5. 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?

    2. Is there a demo somewhere (or walkthrough article/tutorial) that I can look at?
    3. 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)?
    4. 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...

    #42595
    Digital RockeryDigital Rockery
    Participant

    I just noticed something that might be helpful: When echoing get_queried_object_id() and $post_id:

    1. 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)
    2. $post_id return nothing
    3. This behavior is the same whether the test echos are placed directly after the shortcode or after the MB Frontend Submissions form
    #42609
    PeterPeter
    Moderator

    That'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.

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.