WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Query Custom Taxonomies (4 posts)

  1. extatix
    Member
    Posted 4 years ago #

    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?

  2. MichaelH
    Member
    Posted 4 years ago #

    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();
      }
    }
    ?>
  3. extatix
    Member
    Posted 4 years ago #

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

  4. extatix
    Member
    Posted 3 years ago #

    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]

Topic Closed

This topic has been closed to new replies.

About this Topic