Support

Account

Home Forums General Issues Relationship Code Problem

Solved

Relationship Code Problem

    • homax

    • July 22, 2013 at 6:58 pm

    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?

    • homax

    • July 22, 2013 at 11:38 pm

    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.

    • Elliot

    • July 23, 2013 at 3:30 pm

    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

    • homax

    • July 23, 2013 at 4:38 pm

    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?

    • Elliot

    • July 24, 2013 at 12:23 pm

    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; ?>
    
    • homax

    • July 24, 2013 at 4:48 pm

    Hi elliot,

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

    Regards,

    Andreas

    • Elliot

    • July 25, 2013 at 1:03 pm

    Have you attempted to debug the issue?

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

    • July 25, 2013 at 4:53 pm

    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
    )

    )

    • Elliot

    • July 26, 2013 at 3:01 pm

    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; ?>
    
    • homax

    • July 26, 2013 at 5:29 pm

    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.

    • Elliot

    • July 29, 2013 at 1:24 pm

    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?

    • homax

    • July 29, 2013 at 4:51 pm

    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.

    • Elliot

    • July 30, 2013 at 12:50 pm

    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.

    • homax

    • August 20, 2013 at 4:25 am

    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.

    • homax

    • August 22, 2013 at 7:32 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.