Support

Account

Home Forums General Issues Reverse Relationship Meta Query Reply To: Reverse Relationship Meta Query

  • The code is part of the bank CPT single-post template. I’ve tried many iterations, but here’s where it currently stands:

    <?php
    /*
     * Template Name: Bank Review Test
     * Template Post Type: banking
     */
    
    add_action ( 'genesis_before_entry_content', 'banking_data');
    
    function banking_data(){
    
        $post_id = get_the_ID();
    
        $accounts = get_posts(array(
            'post_type'         => array('bank_accounts','money_market_account','checking_accounts'),
            'posts_per_page'    => -1,
            'meta_query'        => array(
                array(
                    'key' => 'bank_relationship', // name of custom field
                    'value' => $post_id, // matches exactly "123", not just 123. This prevents a match for "1234"
                    'compare' => 'LIKE',
                )
            )
            ));
    
            if( $accounts ): 
            ?>
                <ul>
                <?php foreach( $accounts as $account ): ?>
                
                    <li>
                            <?php echo get_the_title( $account->ID ); ?>
                        </a>
                    </li>
                <?php endforeach; ?>
                </ul>
            <?php endif;
    
    }
    
      genesis();
    
    ?>

    Note that the 3 post types all use the same ACF relationship field that points to the bank CPT. I’ve also tried hard-coding the post ID with no success.