This topic contains 3 replies, has 2 voices, and was last updated by  Steve 4 years, 11 months ago.

  • Author
    Posts
  • #2665

    sajonara
    Participant

    As I searched for some time and only found some outdated solutions, I then tinkered with the code and managed to get a solution working to provide a multi select box for choosing relationships for a custom post type.

    
    piklist(
        'field',
        array(
            'type' => 'select',
            'scope' => 'post_meta',
            'field' => 'your field name',
            'label' => __('your label'),
            'description' => __('your desc'),
            'attributes' => array(
                'class' => 'css class',
                'multiple' => 'multiple'
            ),
            'choices' => piklist(
                get_posts(
                    array(
                        'post_type' => 'your post type which should get related to another post',
                        'hide_empty' => false
                    )
                ),
                array(
                    'ID',
                    'post_title'
                )
            )
        )
    );
    

    This way it saves the ID of the related posts as a post meta.

  • #2667

    Steve
    Keymaster

    @sajonara– Piklist has a post-to-post relationship field. But this is a great solution as well. Thanks for sharing!

  • #2668

    sajonara
    Participant

    You’re welcome.

    The post-to-post-relation field did not fit my needs as I have many custom post type entries and wanted to select multiple of these without activating dozens of check boxes. 🙂 Perhaps you could consider offering different layouts for the post-to-post-relationship field some time in the future.

    Kind regards.

  • #2722

    Steve
    Keymaster

    @sajonara– It only our list of todos. Thanks again! Closing ticket.

The topic ‘Add relationships for custom post types’ is closed to new replies.