Home Forums Front-end Issues Relationships with cf "Post object"


Relationships with cf "Post object"

  • Hello!

    I have 3 post types (banks, developers, building).

    Post in post type “building” have post object custom field, that refers to banks. (There I bring the banks that issue mortgage on the building).

    On single page “banks” I was able to display posts. But i have a trouble.

    I can not get post on single bank page, which referred to the bank from post type “building”.

    Example (I have the following links):

    Building (post type):
    First building -> First bank, second bank
    Second building -> Second bank
    Third building -> First bank

    I want to on the banks of the page displays the following information:

    First bank -> First building, Third building
    Second bank -> First building, Second building

    That is, the relationships that I have pointed out previously.

    In post type “building” i use custom field “ns_zastroyschik”.

    Sorry for my English.

  • I find the solution!

    						*  Query posts for a relationship value.
    						*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    						$buildings = get_posts(array(
    							'post_type' => 'novostroyki',
    							'meta_query' => array(
    									'key' => 'ns_zastroyschik', // name of custom field
    									'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    									'compare' => 'LIKE'
    						<?php if( $buildings ): ?>
    							<?php foreach( $buildings as $building ): ?>
    										<?php echo get_the_title( $building->ID ); ?>
    							<?php endforeach; ?>
    						<?php endif; ?>


Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Relationships with cf "Post object"’ is closed to new replies.