WordPress.org

Forums

help with custom-taxonomy-related-posts-query (3 posts)

  1. marcie73
    Member
    Posted 2 years ago #

    Hello,
    I'm pulling out a thread closed almost 1 year ago.
    http://wordpress.org/support/topic/custom-taxonomy-related-posts-query?replies=28
    I'm trying to show related posts by a custom taxonomy. I adapted some of the codes published by the members and managed to make it work, apart from 'orderby' => 'rand'.

    This is the code:

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'cliente', 'string');
    $do_not_duplicate[] = $post->ID;
    
    if(!empty($terms)){
        foreach ($terms as $term) {
            query_posts( array(
            'cliente' => $term->slug,
            'showposts' => 5,
            'caller_get_posts' => 1,
    		'orderby' => 'rand',
            'post__not_in' => $do_not_duplicate ) );
            if(have_posts()){
                while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
    
                <li class="articlecontextual">
                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
                <?php the_ID(); ?>"
                    <h2><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    
                </li>
                <?php endwhile; wp_reset_query();
            }
        }
    }
    ?>

    Please anyone can help me to understand what missing or wrong?
    Thank you!

  2. marcie73
    Member
    Posted 2 years ago #

    Follow this issue
    http://stackoverflow.com/questions/10918814/wordpress-query-posts-orderby-rand-not-working-in-category-archive-template
    I Solved it. Maybe it is not perfect but it seems to work fine.
    After
    global $post;
    I added
    remove_all_filters('posts_orderby');
    and that's all, maybe it could be helpful to someone else...

  3. Tommy Pradana
    Member
    Posted 2 years ago #

    try this, use tag taxonomy called album_tag on my theme, you can changet to your taxonomy to be show....

    <?php $orig_post = $post;
    global $post;
    if ( 'YOUR_POSTTYPE' == get_post_type() ) {
    $tags = wp_get_post_terms($post->ID, 'YOUR_TAXONOMY');
    if ($tags) {
    	$tag_ids = array();
    
    	foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    	$args=array(
    		'tax_query' => array(
            array(
                'taxonomy'  => 'YOUR_TAXONOMY',
                'terms'     => $tag_ids,
                'operator'  => 'IN'
            )
        ),
        'post__not_in'          => array( $post->ID ),
        'posts_per_page'        => 3,
        'ignore_sticky_posts'   => 1
    	);
    	$my_query = new wp_query( $args );
    
    	if( $my_query->have_posts() ) { ?>
    		<div class="related">
    			<h2><?php esc_html_e('Related Products','eStore'); ?></h2>
    			<ul class="related-items clearfix">
    				<?php $i=1; while( $my_query->have_posts() ) {
    				$my_query->the_post(); ?>
    
    					<li<?php if($i%2==0) echo(' class="second"'); ?>>
    						<a href="<?php the_permalink(); ?>" class="clearfix">
    
    							<span><?php the_title(); ?></span>
    						</a>
    					</li>
    					<?php $i++; ?>
    				<?php } ?>
    			</ul>
    		</div>
    	<?php }
    }
    }
    $post = $orig_post;
    wp_reset_query(); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic