• Resolved extatix

    (@extatix)


    Hi. I’m using a custom taxonomy called ‘relations’ to do a poor man’s related posts version (since I already use a related posts plugin for another purpose). I’ve set up a custom query to display posts sharing the ‘relation’ but when a posts has two relations it horribly fails (displays nothing).

    This is the code I use:

    function related_mess() {
    $relation = get_the_term_list( $post->ID, 'relations',' ', ', ', '' );
    if(!empty($relation)) { ?>
    <div stuff>
    <?php $rmquery =  new WP_query(array('relations' => $relation, 'showposts' =>10, 'post__not_in' => array($exclude)));
    while ( $rmquery->have_posts()) : $rmquery->the_post(); ?>
    [do stuff]

    I know where it goes wrong, it’s simply ‘relations’ => wants a string and doesn’t really get it. How do I fix this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Actually you would need to iterate through the $relations object and get the posts for each ‘relations’ term. Something like:

    <?php
    //for a given taxonomy, get the terms on this post, then get related posts. Also don't include the 'current post'
    $tax = 'relations';
    $tax_terms = wp_get_post_terms( $post->ID , $tax, '');
    $post_type = 'any';
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post__not_in' => array($post->ID),
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of '.$post_type . ' where the taxonomy '. $tax . '  is '. $tax_term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
        }
        wp_reset_query();
      }
    }
    ?>
    Thread Starter extatix

    (@extatix)

    Ah, I see. That piece of coding didn’t work either but I’ll look if I can make a ‘blend’ of it.

    Thread Starter extatix

    (@extatix)

    Replying to my old mess, this is how I actually ‘fixed’ it:

    function related_mess_revisited() {
    		global $wp_query;
    		$exclude = $wp_query->post->ID;
    		$all_relations = get_the_term_list( $post->ID, 'relations',' ', ', ', '' );
    		$relations = explode(",", $all_relations);
    		if (!empty($all_relations)) { ?>
    		Related to: <?php
    		foreach ($relations as $relation) {
    				$args = array(
    				'relations' => $relation,
    				'showposts' => 10,
    				'post__not_in' => array($exclude),
    				); ?>
    		<ul class="related-mess">
    		<?php $rmquery = new WP_query($args);
    		if( $rmquery->have_posts() ) {
    		while ( $rmquery->have_posts()) : $rmquery->the_post(); ?>
    [do stuff]
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Query Custom Taxonomies’ is closed to new replies.