Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Eric Mayfield
Eric Mayfield
3,312 Points

Strange issue where custom post relationship will only load the latest 5 posts.

I have 2 custom post types: 1. Artists 2. Artwork. Currently I have the page single-artist.php setup so that it will load Art which is associated with a particular Artist.

My code works great except that it will only load the latest 5 Artworks unless I provide the parameter 'posts_per_page' => (an integer).

If I specify a number less than the number of Art for an Artist it will will only show that, like mentioned before if I don't have this parameter it will only show the latest 5 Art posts, and my workaround is to specify either the exact number of posts available for Art or an arbitrary number greater than the posts available.

Currently I have it set to 50 and it works great, but I still feel like this is an issue because it should load the exact number of Art posts which are available dynamically it seems.

You can see the example in action @ http://www.framesandartunlimited.com/artist/Sterling-Everett/

My code from single-artist.php is as follows:

```<?php get_header(); ?>

<div id="content"> <div class="wrapper">

    <!-- Check if there is no content -->
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <div class="entry">
    <div class="featured_image">
        <img src="<?php the_field( 'picture' ); ?>" />
    </div>
    <div class="content_column">
        <h1>
            <?php the_field( 'name' ); ?>
        </h1>
        <p><?php the_field( 'description' ); ?></p>

        <?php 

            $artwork = get_posts( array (
                    'post_type' => 'artwork',
                    'posts_per_page' => 50,
                    'meta_query'=> array (
                        array (
                            'key' => 'artist',
                            'value' => '"' . get_the_ID() . '"',
                            'compare' => 'LIKE'
                        )
                    )
            ));
        ?>

        <?php if( $artwork): ?>


        <div class="gallery-wrap">
            <?php foreach( $artwork as $art ): ?>
                <?php 
                    $photo = get_field( 'image', $art->ID);
                    $title = get_field( 'name', $art->ID );
                ?>
                <?php 
                    $width = $photo[ 'width' ];
                    $height = $photo [ 'height' ];
                ?>

                <?php if( $width > $height ) : ?>

                    <div class="gallery-item">
                        <?php

                            $scale =  220 / $width;
                            $scaled_height = round($height * $scale);
                            $overall_margin = 220 - $scaled_height;
                            $margin = $overall_margin / 2;

                            if( $margin % 2 == 0 )
                                $margin = $margin - 0.5;


                            if( $scaled_height % 2 == 0 ):
                                $margin_top = $margin;
                                $margin_bottom = $margin;
                            else:
                                $margin_top = $margin;
                                $margin_bottom = $margin + 1;
                            endif;


                        ?>

                        <a href="<?php echo get_permalink( $art->ID ); ?>"><img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" width="220" style="margin-top:<?php echo $margin_top; ?>px; margin-bottom:<?php echo $margin_bottom; ?>px;"></a>
                        <div class="gallery-item-caption">
                            <p>
                                <a href="<?php echo get_permalink( $art->ID ); ?>" title="caption"><?php echo $title ?></a>
                            </p>
                        </div>
                    </div>
                <?php elseif( $height > $width ) : ?>

                     <div class="gallery-item">
                        <a href="<?php echo get_permalink( $art->ID ); ?>"><img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" height="220"></a>
                        <div class="gallery-item-caption">
                            <p>
                                <a href="<?php echo get_permalink( $art->ID ); ?>" title="caption"><?php echo $title ?></a>
                            </p>
                        </div>
                    </div>
                <?php else: ?>

                    <div class="gallery-item">
                        <a href="<?php echo get_permalink( $art->ID ); ?>"><img src="<?php echo $photo['url']; ?>" alt="<?php echo $photo['alt']; ?>" width="220" height="220"></a>
                        <div class="gallery-item-caption">
                            <p>
                                <a href="<?php echo get_permalink( $art->ID ); ?>" title="caption"><?php echo $title ?></a>
                            </p>
                        </div>
                    </div>

                <?php endif; ?>   

            <?php endforeach; ?>
        </div>

    <?php endif; ?>

    </div>
    </div>
    <!-- Check if there is no content -->
    <?php endwhile; else: ?>

        <p>There are no posts or pages here</p>

    <?php endif; ?>
    <div class="clear"></div>
</div>

</div> <?php get_footer(); ?>```

2 Answers

Anthony Moore
Anthony Moore
2,282 Points

If you set 'posts_per_page' to '-1-, it will load all of the posts.

Eric Mayfield
Eric Mayfield
3,312 Points

Worked like a charm, you're the man Anthony!!!