Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom-post-type named my_projects and a custom-taxonomy called project_tags.

I want to write a function that is returning 5 posts of this post-type based on a given taxonomy term.

So I want to call <?php get_related_projects("Car"); ?> and get 5 posts that also share the tag car returned.

This is my current attempt (that doesn't work)

function get_related_projects( $term ) {
    echo '<ul class="featured-items">';

    $args = array(
        'post_type' => array('my_project'),
        'order' => 'DSC',
        'post_count' => 5,
        'meta_key' => 'project_tags',
        'meta_compare'   => $term,
    );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
        global $post;
        ?>
        <li>
            <figure class="project-image">
                <?php if ( has_post_thumbnail()) : ?>
                   <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                    <?php the_post_thumbnail(); ?>
                   </a>
                 <?php endif; ?>
            </figure>
            <h1 class="title"><a href="<?php the_permalink(); ?>"><span class="goto">a</span> <?php the_title(); ?></a></h1>
        </li>
        <?php
    endwhile;

    wp_reset_postdata();
    echo '</ul>';
}

Since I'm not an expert when it comes to custom loops I wonder what I'm doing wrong here? Thank you for your help.

UPDATE:

<?php if ( get_related_projects( "Car" ) ):  ?>
        <em>Related Projects</em>
        <?php get_related_projects("Car"); ?>
<?php endif; ?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Change your $args array a little bit:

$args = array(
    'post_type' => array( 'my_projects' ), // it need to get the posts from this post_type
    'order' => 'DESC',
    'posts_per_page' => 5,
    'project_tags' => $term // search taxonomy
);

Other ways to search for taxonomy terms can be found here in the WordPress Codex pages.

Answer on your update:

You can add checks that return false when the function "can't" return values:

// if the given category doesn't exist
if ( ! is_category( $term ) )
    return false;

// if the given category doen't have posts
if ( ! $loop->have_posts() )
    return false;

The way you set up your code to check if there are related projects will not work the way you set up the function now, try this:

function get_related_projects( $term ) {
    // if the given category doesn't exist
    if ( ! is_category( $term ) )
        return false;

    $return = '';
    $return .= '<ul class="featured-items">';

    $args = array(
        'post_type' => array( 'my_projects' ), // it need to get the posts from this post_type
        'order' => 'DESC',
        'posts_per_page' => 5,
        'project_tags' => $term // search taxonomy
    );

    $loop = new WP_Query( $args );

    // if the given category doen't have posts
    if ( ! $loop->have_posts() )
        return false;

    while ( $loop->have_posts() ) : $loop->the_post();
        global $post;

        $figure = '';
        if ( has_post_thumbnail()) :
            $figure = sprintf( '<a href="%1$s" title="%2$s">%3$s</a>',
                get_permalink( get_the_ID() ),
                the_title_attribute( array( 'echo' => false ) ),
                get_the_post_thumbnail( get_the_ID() )
            );
        endif;

        $return .= sprintf( '
            <li>
                <figure class="project-image">%1$s</figure>
                <h1 class="title"><a href="%2$s"><span class="goto">a</span> %3$s</a></h1>
            </li>',
            $figure,
            get_permalink( get_the_ID() ),
            get_the_title()
        );
    endwhile;

    wp_reset_postdata();
    $return .= '</ul>'

    return $return;
}

And then use the following code to check the function:

<?php if ( $projects = get_related_projects( "Car" ) ):  ?>
    <em>Related Projects</em>
    <?php echo $projects; ?>
<?php endif; ?>
share|improve this answer
    
Thank you, perfect. One more question. Is it possible to rewrite the function so, that I have an if-statement in there that returns false if no posts where matched? So I want to use the function in a second different manner. See my update in the question. -> So I don't want to print the section "Related Projects" if there are no projects matched. –  mathiregister Jan 14 '13 at 11:30
    
I edited my answer for you :-) –  Mike Madern Jan 14 '13 at 11:59
    
Wow, thank you. Exactly what I needed. In this case I ask even for the last thing I'd like to have. Is it also possible to not only return the "last" 5 posts, but a random set of the posts. So I want 5 related projects, but not simply the latest ones but a random set. –  mathiregister Jan 14 '13 at 12:10
    
Just add 'orderby' => 'rand' in your $args array and remove 'order' => 'DESC' ;-) –  Mike Madern Jan 14 '13 at 12:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.