0

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

1 Answer 1

1

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; ?>
4
  • 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. Commented Jan 14, 2013 at 11:30
  • I edited my answer for you :-) Commented Jan 14, 2013 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. Commented Jan 14, 2013 at 12:10
  • Just add 'orderby' => 'rand' in your $args array and remove 'order' => 'DESC' ;-) Commented Jan 14, 2013 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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