Relationship CPT Title not Displaying using MB View Shortcode

Support MB Relationships Relationship CPT Title not Displaying using MB View ShortcodeResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #28281
    ShriniShrini
    Participant

    Basically want to display list of candidate & his party

    Relationships:
    1. Candidate belongs to one party
    2. Party have many members (candidates)

    so created a relationship called "candidate-party" between candidate CPT & party CPT (non-reciprocal)

    Relationship Establishment Reference:

    <?php
    add_action( 'mb_relationships_init', 'candidate-party' );
    
    function candidate-party() {
        MB_Relationships_API::register( [
            'id'   => 'candidate-party',
            'from' => [
                'object_type'   => 'post',
                'post_type'     => 'candidate',
                'empty_message' => 'No Party Added',
                'admin_column'  => [
                    'position' => 'after title',
                    'link'     => 'view',
                ],
                'meta_box'      => [
                    'title' => 'Party',
                ],
                'field'         => [
                    'add_button' => 'Add Member',
                ],
            ],
            'to'   => [
                'object_type'   => 'post',
                'post_type'     => 'party',
                'empty_message' => 'No Candidates Added',
                'meta_box'      => [
                    'title'   => 'Party Members',
                    'context' => 'normal',
                ],
                'field'         => [
                    'max_clone' => '1',
                ],
            ],
        ] );
    }

    as a first step trying to display list of candidates through MB View using shortcode, but unfortunately display none.

    MB View Reference:

    Candidate List:

    
    {% set relationship = attribute( relationships, 'candidate-party' ) %}
    {% for post in relationship.to %}
        {{ post.title }}
    {% endfor %}
    
    <br/>
    <br/>
    
    {% set relationship = attribute( relationships, 'candidate-party' ) %}
    {% for post in relationship.from %}
        {{ post.title }}
    {% endfor %}
    
    <br/>
    <br/>
    
    #28285
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Your code looks good except for one point, the function name should use the underscore (_) between words, no hyphen (-). See more here https://www.codementor.io/@veenitchauhan/basics-of-naming-conventions-for-php-developers-eliexmew6

    add_action( 'mb_relationships_init', 'candidate_party' );
    
    function candidate_party() {

    And if you want to use the View shortcode on other pages, please wrap the relationship PHP code in a function then call it via proxy mb. in View.

    #28294
    ShriniShrini
    Participant

    I pasted the generated code from relationship in theme main function file through theme editor in wordpress but it shows

    Your PHP code changes were rolled back due to an error on line 69 of file wp-content/themes/Extra/functions.php. Please fix and try saving again.

    syntax error, unexpected '<', expecting end of file

    
    /*** Meta Box Functions by Shrini  ***/
    
    <?php
    add_action( 'mb_relationships_init', 'candidate_party' );
    
    function candidate_party() {
        MB_Relationships_API::register( [
            'id'   => 'candidate_party',
            'from' => [
                'object_type'   => 'post',
                'post_type'     => 'candidate',
                'empty_message' => 'No Party Added',
                'admin_column'  => [
                    'position' => 'after title',
                    'link'     => 'view',
                ],
                'meta_box'      => [
                    'title' => 'Party',
                ],
                'field'         => [
                    'add_button' => 'Add Member',
                ],
            ],
            'to'   => [
                'object_type'   => 'post',
                'post_type'     => 'party',
                'empty_message' => 'No Candidates Added',
                'meta_box'      => [
                    'title'   => 'Party Members',
                    'context' => 'normal',
                ],
                'field'         => [
                    'max_clone' => '1',
                ],
            ],
        ] );
    }
    

    so I removed the <?php and pasted but it shows

    
    Your PHP code changes were rolled back due to an error on line 101 of file wp-content/themes/Extra/functions.php. Please fix and try saving again.
    
    syntax error, unexpected end of file
    
    #28296
    ShriniShrini
    Participant

    Ok fixed the syntax error & saved successfully

    #28298
    ShriniShrini
    Participant

    still not able to display the values

    here is the MB View
    code

    
    Candidate List:
    
    {% set relationship = attribute( relationships, 'candidate_party' ) %}
    {% for post in relationship.to %}
        {{ post.title }}
    {% endfor %}
    
    <br/>
    <br/>
    
    {% set relationship = attribute( relationships, 'candidate_party' ) %}
    {% for post in relationship.from %}
        {{ post.title }}
    {% endfor %}
    {% for post in relationships.candidate_party.to %}
        {{ post.title }}
    {% endfor %}
    {% for post in relationships.candidate_party.from %}
        {{ post.title }}
    {% endfor %}
    <br/>
    <br/>
    
    #28307
    ShriniShrini
    Participant

    Here is the video for your reference ....just simple task, I dont know where I am making mistake

    https://www.loom.com/share/09beaf22c6e64e4b8d7b817cc6d0e2c0

    #28316
    Long NguyenLong Nguyen
    Moderator

    Hi Shrini,

    Let me explain this case.

    If you use Insert Field and get the code

    {% set relationship = attribute( relationships, 'candidate_party' ) %}
    {% for post in relationship.from %}
        {{ post.title }}
    {% endfor %}

    it will take the current post ID of the post that you insert the shortcode. The PHP code in the documentation has the same meaning

    $pages = MB_Relationships_API::get_connected( [
        'id'   => 'posts_to_pages',
        'from' => get_the_ID(), //here
    ] );
    foreach ( $pages as $p ) {
        echo $p->post_title;
    }
    

    So if you add the shortcode to a post/page that does not have the relationship, it will not show the connection title. You should change the Type to Singular and Post to post type Candidate to use the field in the Insert Fields list. Screenshot https://share.getcloudapp.com/Jru4K55B

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