Support

Account

Home Forums Feature Requests Relationship Fields set as $post->post_parent Reply To: Relationship Fields set as $post->post_parent

  • I was able to get this figured out through a couple stack exchange forums. I did not end up using an ACF field because I wasn’t sure at the time. Essentially I have a rewrite rule hooked to init and post_type_link that uses the meta value of my custom field (which I imagine can be done with ACF as well. Here’s some of the code:

    /* Add rewrite rules for permalink structure */
    add_action( 'init', array( $this, 'map_rewrite_rules') );
    add_filter( 'post_type_link', array( $this, 'filter_map_permalik'), 10, 2 );
    public function setup_map_meta_boxes() {
      add_meta_box('story-map', 'Map Series', 'story_attributes_meta_box', 'story', 'side', 'default');
    }
    
    public function map_rewrite_rules() {
      add_rewrite_rule( '(.*)/([^/]+)/?$','index.php?story=$matches[2]','top' );
    }
    

    I made the custom field like so:

    function story_attributes_meta_box($post) {
      $pages = wp_dropdown_pages(
        array(
          'post_type'         => 'map',
          'selected'          => $post->post_parent,
          'name'              => 'parent_id',
          'show_option_none'  => __('(no parent)'),
          'sort_column'       => 'menu_order, post_title',
          'echo'              => 0
        )
      );
    
      if ( ! empty($pages) ) {
        _e('<label class="fat-wide">Map: </label>');
        echo $pages;
      }
    }

    Hope that can help you on your project in some way