Support

Account

Home Forums Front-end Issues Query to display all posts with ACF relationship value Reply To: Query to display all posts with ACF relationship value

  • Hi!

    First off, move wp_reset_postdata(); right under the endforeach; in your related posts loop.. that should fix the error with the incorrect ID.

    Secondly.. there doesn’t seem to be a default archive link for posts if you don’t include a date or category tag (like http://www.demo.com/2012/). I honestly cant think of a smooth way to solve this the way it’s set up at the moment.. If I where you I’d either rethink it all or id create categories thats named the same as the companies but with a permalink of something like companyname-cat and assign the posts to them as well. That way you could just put this for the readmore link:

    
    <?php
    $cat = get_category_by_slug($post->slug.'-cat');
    $catlink = get_category_link($cat->term_id);
    ?>
    <a href="<?php echo $catlink; ?>">View all</a>
    

    then you can check in archive.php if it’s trying to display a category with

    
    if(is_cat()){
    //the archive is displaying posts from a category
    }
    

    If this solution doesn’t work you could create a custom taxonomy and assign it to the posts (besides categories and tags) and use them instead and change the functions accordingly..

    
    
    <?php
    $cat = get_term_by('slug, '$post->slug.'-cat', 'your-taxonomy-slugname');
    $catlink = get_category_link($cat->term_id);
    ?>
    <a href="<?php echo $catlink; ?>">View all</a>
    
    

    and:

    
    if(is_tax()){
    
    //the archive is displaying a term from a taxoonomy
    }