Forum Replies Created

Viewing 15 replies - 1 through 15 (of 45 total)
  • Were you able to find a solution for this?

    I forgot to mention to place my code in the loop.

    @sevenspark My problem has always been that I want it to randomize how it finds the posts. If i get what you’re saying this is what would happen:

    Say we have a post with 2 tags. If the first tag has 4 or more posts associated with it, it would display those 4 posts, but disregard the second tag.

    @mistercyril

    Here is the code I have thats functioning. It looks for your specified custom taxonomy, and outputs up to 4 posts that have the same tags. The only problem with this code is that it will find 4 posts for EACH term found. I haven’t been able to make it a total of 4 posts.

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'badge', 'string'); //change badge to your taxonomy, these are my tags
    $do_not_duplicate[] = $post->ID;
    $postnum = 0;
    
    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'badge' => $term->slug, //custom taxonomy, these are my tags
            'showposts' => 4,
    	'orderby' => 'rand', //orders results randomly
            'caller_get_posts' => 1,
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){ ?>
    
                <?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    		<?php $postnum++; //generates a unique number for each post and applies it to its class to have theming control ?>
    	<div class="post-<?php echo $postnum; ?>">
          			<?php
    			$clipping_image = get_post_meta( $post->ID, 'clipping_image', true );
    			$terms_of_post = get_the_term_list( $post->ID, 'type', '','', '', '' );//change type to your post type ?>
    
    				<?php if( $terms_of_post ) echo $terms_of_post; ?>
    
    		</div>
            		<?php $found_none = '';
         			endwhile; wp_reset_query();
    ?>
    
        <?php }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>

    *Place code in loop

    I’ve commented some of my code to help you out. I may have left some divs or p tags around from my own code by accident. Please check before using. Let me know if that works.

    Thread Starter fcarthy

    (@fcarthy)

    bump

    fcarthy

    (@fcarthy)

    This is what I’m using and it retrieves the posts great. The only problem is that it retrieves up to 3 related posts PER term, not 3 total. Any ideas how to achieve this?

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'badge', 'string');
    $do_not_duplicate[] = $post->ID;
    $postnum = 0;
    
    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'badge' => $term->slug,
            'showposts' => 3,
            'caller_get_posts' => 1,
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){ ?>
    
                <?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    		<?php $postnum++; ?>
    	<div class="single-small-posts-<?php echo $postnum; ?>">
          			<?php
    			$clipping_image = get_post_meta( $post->ID, 'clipping_image', true );
    			$terms_of_post = get_the_term_list( $post->ID, 'type', '','', '', '' );?>
    
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $clipping_image ?>&h=140&w=140&zc=1"></a>
    				<p class="single-small-post-cat"><?php if( $terms_of_post ) echo $terms_of_post; ?></p><p class="single-small-post-number">#<?php echo get_post_meta($post->ID,'incr_number',true); ?></p>
    
    		</div>
            		<?php $found_none = '';
         			endwhile; wp_reset_query();
    ?>
    
        <?php }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>

    fcarthy

    (@fcarthy)

    Hi,

    I’m using this code to retrieve related posts based on my custom taxonomy. It works great, but the only problem is that I’m trying to retrieve a total of 4 related posts. This retrieves up to 4 posts per term. What am I doing wrong?

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'badge', 'string');
    $do_not_duplicate[] = $post->ID;
    $postnum = 0;
    
    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'badge' => $term->slug,
            'showposts' => 3,
            'caller_get_posts' => 1,
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){ ?>
    
                <?php while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    		<?php $postnum++; ?>
    	<div class="single-small-posts-<?php echo $postnum; ?>">
          			<?php
    			$clipping_image = get_post_meta( $post->ID, 'clipping_image', true );
    			$terms_of_post = get_the_term_list( $post->ID, 'type', '','', '', '' );?>
    
    			<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title();?>"><img src="<?php bloginfo('template_directory'); ?>/scripts/timthumb.php?src=<?php echo $clipping_image ?>&h=140&w=140&zc=1"></a>
    				<p class="single-small-post-cat"><?php if( $terms_of_post ) echo $terms_of_post; ?></p><p class="single-small-post-number">#<?php echo get_post_meta($post->ID,'incr_number',true); ?></p>
    
    		</div>
            		<?php $found_none = '';
         			endwhile; wp_reset_query();
    ?>
    
        <?php }
      }
    }
    if ($found_none) {
    echo $found_none;
    }
    $post = $backup;  // copy it back
    wp_reset_query(); // to use the original query again
    ?>

    fcarthy

    (@fcarthy)

    Thank you for this! it works like a charm. I just have one issue with it. I have multiple terms per post, and this pulls only the first one.

    How can I change this to list all terms?

    Thanks again!

    I have the threaded comments option checked and threaded to 3, but still no reply link… im running 3.0.1

    Thread Starter fcarthy

    (@fcarthy)

    Any help?

    Thread Starter fcarthy

    (@fcarthy)

    That was exactly the problem. Thanks for taking me back to WP 101!!

    I’m having the same issue… what did you change?

    For sure:

    For example. I have my own Custom Post Type named ‘clipping’. I also have my own custom, non-heirerical taxonomy called ‘tag’.

    Say for example I write 3 posts, or “clippings”, and each one is tagged using the ‘tag’ taxonomy like so:

    Post 1: apparel, sun glasses,
    Post 2: apparel, t-shirts,
    Post 3: photography, t-shirts,
    Post 4: art, things,
    Post 5: apparel, stuff,

    When reading Post 2 I should see a list (that I can theme via CSS) of it’s related posts, which in this case would be:

    Post 1
    Post 3
    Post 5

    Does this make sense? I want all posts that have at least 1 term in common with the current post.

    I hear you, but what I’m trying to say is that I don’t have a custom field, I have a custom tags taxonomy. You’re saying that I should start out by using this:

    function get_custom_field($key, $echo = FALSE) {
    	global $post;
    	$custom_field = get_post_meta($post->ID, $key, true);
    	if ($echo == FALSE) return $custom_field;
    	echo $custom_field;
    }

    But I’m trying to get an array of values, not just one. So this:
    $custom_field = get_post_meta($post->ID, $key, true);
    I would think it should look more like this:
    $custom_tags = get_the_term_list( $post->ID, 'customtags',' ', ', ', '' )

    … so that I could find all posts that have 1 or MORE related tags, not all posts that match the 1 field/term.

    Now, if I’m crazy, and this is exactly what you’re telling me to do and how to do it, I apologize! Thanks for your help.

    Yes, correct. I need to find all posts that have 1 or more tags as the current single.php post, then randomize them, then show only 4.

    Thanks! But I’ll be honest I’m a little lost by your example. I’m trying to find all posts tagged with the same term from a custom taxonomy, not from a custom field. I would think I would have to use something like :
    get_the_term_list( $post->ID, 'taxonomyname',' ', ', ', '' )

    How can I apply what you have to get a term list rather than a specific custom field?

Viewing 15 replies - 1 through 15 (of 45 total)