Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter sublimeology

    (@sublimeology)

    So, I have it working. Basically, the terms are a hierarchical list. So, in order to match all the terms/categories, I really just needed a query to find the last “category” it was in (the final child of the hierarchy), and make it only pull the custom posts from that category (with the right CPT).

    JUST IN CASE anyone else is wanting to do something similar, I thought I would post the code here. I’ve commented areas to see what I was doing in the various places to make it a little more understandable.

    <!-- other posts -->
    <?php
    //this gets the lowest hierarchical child term for post
    $categories = get_the_category($post->ID);
    foreach($categories as $category) :
    	$children = get_categories( array ('parent' => $category->term_id ));
    	$has_children = count($children);
    	if ( $has_children == 0 ) {
     	$current_child = $category->name;
    	}
    endforeach;
    //this starts the query for the program listings
    $post_type = 'program_listings';
    $tax = 'category';
    $tax_terms = wp_get_object_terms($post->ID, 'category');
    if ($tax_terms == $category_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'operator' => 'AND',
          'posts_per_page' => 5,//limits the listing to 5
          'caller_get_posts'=> 1,
          'post__not_in' => array ($post->ID),
          'orderby' => 'ID', //orders by ID, you could also tell it to order by: ‘author’ ‘title’‘name’‘date’ or ‘rand’
    'order' => 'ASC', //tells it the display order should be ascending, DSC would indicate a descending order
    
        );
        $my_query = null;
        $my_query = new WP_Query($args);
    	    if( $my_query->have_posts() && $tax_term->name == $current_child)//now only display categories the current post is in
    	     {
          echo '<h2>Other programs in this category</h2><ul>';
          while ($my_query->have_posts() && $tax_term->name==$current_child ) : $my_query->the_post(); ?>
            <li style="margin-left:15px;list-style:none;"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
            <?php
          endwhile;
        }
        wp_reset_query();
      }
    }
    echo '</ul>';
    ?>
    
    <!-- end other posts -->

    Hope that helps someone else – this was something that I found alot of questions about how to do but not very many answers.

    Thread Starter sublimeology

    (@sublimeology)

    So, I’ve got the code to display posts from each of the relevant categories but I can’t seem to figure out that last step to get it to show only the custom posts from ALL the relevant categories (one list).

    Here’s the new code (still with the troubleshooting term list at the top):

    <!-- other posts -->
       <?php
         //get the post's terms (troubleshooting - can be removed once figured out!
    $category_terms = wp_get_object_terms($post->ID, 'category');
    if(!empty($category_terms)){
      if(!is_wp_error( $category_terms )){
        echo 'Terms <ul>';
        foreach($category_terms as $term){
          echo '<li><a href="'.get_term_link($term->slug, 'category').'">'.$term->name.'</a></li>';
        }
        echo '</ul>';
      }
    } //get post terms done
    ?>
     <?php
    //for a given post type, return all
    $category_terms = wp_get_object_terms($post->ID, 'category');
    $post_type = 'program_listings';
    $tax = 'category';
    $tax_terms = wp_get_object_terms($post->ID, 'category');
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'operator' => 'AND',
          'posts_per_page' => 5,
          'caller_get_posts'=> 1
    
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
                if( $my_query->have_posts() ) {
          echo '<h2>Other programs in the '. $tax_term->name. ' category</h2> ';
          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();
      }
    }
    
    ?>
    
    <!-- end other posts -->
    Thread Starter sublimeology

    (@sublimeology)

    yes, using the and operator, it shows all the regular posts in that category – not just the custom post types that contain those terms.

    I need it to display only posts that are in the “program_listings” custom post type that match ALL categories of current post.

    I’ve tried pulling the function out of the top part of the code (that is simply echoing the terms right now) into the bottom part of the code (that is displaying posts) but still can’t get it to work.

    I feel like maybe I should be able to do something like this:

    (starting at line 34):

    if ($related_items->have_posts()) :
     foreach($category_terms as $term){
     echo '<h2>Other Programs in this category</h2><ul>';
        while ( $related_items->have_posts() ) : $related_items->the_post();}
        ?>
            <li style="margin-left:10px;list-style:none;"><a href="<?php the_permalink(); ?>"
                       title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?></a></li>

    In my head, this should tell it to find related posts, match it with the $category_terms, and the display the list. But, that gives me a parse error.

Viewing 3 replies - 1 through 3 (of 3 total)