Support

Account

Home Forums General Issues Relationship Code Problem

Solved

Relationship Code Problem

  • Hi I wan’t to display the Title and Link of a Relationship for each post at a category page.

    I tried this code but it doesn’t work. Can someone please help me

    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    <?php setup_postdata($post); ?>
    "><?php the_title(); ?>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

  • have you switched out ‘myfieldname’ for your relationship fields name?

  • Hi, yes I have insert there allready my field name.

    But at the Post Listing for all Posts of the category the second post which would be loaded after this code won’t be published.

  • Hi @homax

    Are you able to re-describe you last reply. You have used quite a few terms which are miss leading to me.

    Thanks
    E

  • Hi,

    sure I wan’t to explain it more detailed.

    I have got a category where now 4 articles should be listed. On every article I would like to display via the relationship the title of an other article which is connected via the relationship.

    For displaying the title I use this code snipped

    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    <?php setup_postdata($post); ?>
    "><?php the_title(); ?>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>

    The title would be displayed perfect.

    But the other 3 Postings in this category won’t be published. Only the first article will be showen in the category. All articles which will be listed after the relationship snipped won’t be showen.

    Have you got any idea?

  • Hi @homax

    I think the issue is the wp_reset_postdata() is reseting the global query back to something other than the expected ‘category posts’.

    I would modify your code to not use the wp_reset_postdata() function. You can do this like so:

    
    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    	<?php foreach( $posts as $p): ?>
    		<?php echo get_the_title( $p->ID ); ?>
    	<?php endforeach; ?>
    <?php endif; ?>
    
  • Hi elliot,

    thanks for your answer. I testet it with this code. But it doesn’t work too.

    Regards,

    Andreas

  • Have you attempted to debug the issue?

    
    $posts = get_field('myfieldname');
    
    echo '<pre>';
    	print_r( $posts );
    echo '</pre>';
    die;
    
  • Hi,

    when I put this code inside I get this response. I have made some changes at the Content and Title because I won’t post this here.

    Have you any Idea what is wrong here?


    Array
    (
    [0] => WP_Post Object
    (
    [ID] => 61
    [post_author] => 2
    [post_date] => 2013-07-19 16:04:59
    [post_date_gmt] => 2013-07-19 14:04:59
    [post_content] => My Content I have writen
    [post_title] => My Title of the post
    [post_excerpt] =>
    [post_status] => publish
    [comment_status] => open
    [ping_status] => open
    [post_password] =>
    [post_name] => my-title-of-the-post
    [to_ping] =>
    [pinged] =>
    [post_modified] => 2013-07-19 16:05:21
    [post_modified_gmt] => 2013-07-19 14:05:21
    [post_content_filtered] =>
    [post_parent] => 0
    [guid] => http://www.domain.de/?post_type=hotel&p=61
    [menu_order] => 0
    [post_type] => hotel
    [post_mime_type] =>
    [comment_count] => 0
    [filter] => raw
    )

    )

  • Hi @homax

    Your result is an array containing post objects. This code should work:

    
    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    	<?php foreach( $posts as $p): ?>
    		<?php echo get_the_title( $p->ID ); ?>
    	<?php endforeach; ?>
    <?php endif; ?>
    
  • Hi,

    with this code I have the Problem which I descriped here.
    http://support.advancedcustomfields.com/forums/reply/reply-to-relationship-code-problem-4/

    Would it make sense when I send you via E-Mail an account for my website that you can see there the problem live?

    Thank you for your help.

  • Hi @homax

    You seem to be ignoring my advice to not use the setup_postdata function / wp_reset_postdata()

    Are you able to use the code I supplied above?

  • Hi,

    yes I have implementet this code


    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    <?php foreach( $posts as $p): ?>
    <?php echo get_the_title( $p->ID ); ?>
    <?php endforeach; ?>
    <?php endif; ?>

    But I get the following Problem:

    The title would be displayed perfect.

    But the other 3 Postings in this category won’t be published. Only the first article will be showen in the category. All articles which will be listed after the relationship snipped won’t be showen.

  • Hi @homax

    So, if you were to now remove the above code, your archive page will load as expected?

    I think there is something fishy going on in your theme in regards to the wp posts loop.

    Perhaps you have a pre_get_posts filter or one of the many other filters in WP / ACF which is modifying the $query.

    It is very easy to accidentally break the global $query in WP and I believe this is what is happening to you.

    Maybe in your code, you are hooking in somewhere and performing a new WP_Query object? If so, make sure you are resetting the query afterwards.

    It may be beneficial to create a new WP install on your local server and just install ACF. Create the same field and edit the twenty12 theme to use this $posts loop. It should work and hopefully give you an idea of where the issue must stem from.

  • Hi,

    yes when I remove the code it would load perfect.

    I have tested it with the “twentythirteen” standard Theme and there the following code doesn’t work too.


    <?php $posts = get_field('myfieldname');
    if( $posts ): ?>
    <?php foreach( $posts as $p): ?>
    <?php echo get_the_title( $p->ID ); ?>
    <?php endforeach; ?>
    <?php endif; ?>

    I have also asked the Developer of the theme I normaly use but has also no idea what is going wrong there. But with the theme there is no error because with the standardtheme it also doesn’t work.

    Have you got any idea what can be done? Can I send you the website via private message that you can take a look what is going wrong there.

    Thank you for your help.

  • Hi,

    I don’t know why my last Post is marked that this is the solution because it doesn’t work.

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

The topic ‘Relationship Code Problem’ is closed to new replies.