3

I have custom post type name "resources" and taxonomy called "type" with lot of terms in it. I do not want to create custom template for each term like taxonomy-type-{term}.php every time I add new term.

What I am trying to achieve here is a single page where it handle to check each terms. If the current term is "24622", show content and so on, but I want it dynamic so I don't want to input term ID each time a new term created.

The code that I use that works so far for single term is this:

<?php
$args = array (
'post_type' => 'resources',
'tax_query' => array(
    array(
    'taxonomy' => 'type',
    'field' => 'id',
    'terms' => 24622 //I WANT IT DYNAMIC
     )
  )
);
$loop = new WP_Query($args);
     if($loop->have_posts()) {
    $term = $wp_query->queried_object;
     while($loop->have_posts()) : $loop->the_post();
        //Output what you want
   echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
}
?>

Please help. Thanks!

UPDATE

Here is the code that works for now but it fetches posts wrong order.

// get all terms used by current post for specific category
$terms = get_the_terms(get_the_ID() , 'type');
// if $terms is array convert array of term objects to array of term IDs
if(is_array($terms)){
$term_ids = wp_list_pluck($terms, 'term_id');
foreach($terms as $term) {
    $post_ids[] = $term->term_id;
}
// proceed with tax query
$args = array(
    'post_type' => 'resources',
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'id',
            'terms' => $term->term_id
        )
    )
);
$loop = new WP_Query($args);

if ($loop->have_posts()) {
    $term = $wp_query->queried_object;
    while ($loop->have_posts()):
        $loop->the_post();

        // Output what you want

        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    endwhile;
}
wp_reset_postdata();
}

enter image description here

If I click the "Case studies (2)" it supposed to show 2 posts but show 1 post. In "Advocacy maps (1)", it shows 2 posts.

Post 1 is related to "Case studies" and "Advocacy maps" terms
Post 2 is related to "Case studies" and "Land monitoring reports"

5
  • "Check each terms" against what, there must be more to your single page than the above code? Where will the dynamic term come from?
    – Malisa
    Sep 9, 2016 at 17:23
  • If im viewing "management" term, the code should compare if the current page is management term. This will show all post under management term. The above code only check 1 term (24622) but I want it dynamically check the term first and show post under that term. No more content above that code.
    – MightyGas
    Sep 9, 2016 at 19:52
  • Where you have change the tax query term to 'terms' => $term->term_id, this will now only search the term id, being the last term the foreach loop found. If you change it to the $post_ids, it will search an array of ID's found, the same as the wp_list_pluck. Something is further wrong here and needs looking into further.. maybe the rest of your code.
    – Malisa
    Sep 10, 2016 at 18:38
  • The three links image you posted, where is this displayed, on which page?
    – Malisa
    Sep 10, 2016 at 18:39
  • It display in custom taxonomy page taxonomy-type.php
    – MightyGas
    Sep 10, 2016 at 19:19

2 Answers 2

1

Thank you for your time answering my unclear question. The code I found is the one that Im looking for. Thank you again :)

<?php 
//http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters
$term_slug = get_queried_object()->slug;
        if ( !$term_slug )
        return;
        else
$args = array(
    'tax_query' => array(
                     array(
                       'taxonomy' => 'gallery_category',
                       'field' => 'slug',
                       'terms' => $term_slug,
                       'posts_per_page' => 10
                     )
                   )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="entry-content">
    <?php the_excerpt(); ?>
  </div><!-- .entry-content -->
<?php endwhile; // End the loop. ?>
0

Well, you could try the following code:

// get all terms used by current post for specific category
$terms = get_the_terms(get_the_ID() , 'type');
// if $terms is array convert array of term objects to array of term IDs
if(is_array($terms)){
$term_ids = wp_list_pluck($terms, 'term_id');
// proceed with tax query
$args = array(
    'post_type' => 'resources',
    'tax_query' => array(
        array(
            'taxonomy' => 'type',
            'field' => 'id',
            'terms' => $term_ids
        )
    )
);
$loop = new WP_Query($args);

if ($loop->have_posts()) {
    $term = $wp_query->queried_object;
    while ($loop->have_posts()):
        $loop->the_post();

        // Output what you want

        echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
    endwhile;
}
wp_reset_postdata();
}

This will return all posts having the same term OR terms as current post. Can try and let me know if OK

7
  • Hi malisa, thanks for you time. However, the code didnt work. It show blank. I also tried changing the code and still no success.
    – MightyGas
    Sep 10, 2016 at 16:03
  • What did you change.. I just saw there is a error on my behalf in the first part.. $terms = get_the_terms(get_the_ID() , 'category'); Should be $terms = get_the_terms(get_the_ID() , 'type');
    – Malisa
    Sep 10, 2016 at 16:14
  • put this in your footer and see if anything is output. $terms = get_the_terms(get_the_ID() , 'type'); print_r($terms);
    – Malisa
    Sep 10, 2016 at 16:16
  • Somehow it works by adding the <code>foreach($terms as $term) { $post_ids[] = $term->term_id; } and change the 'terms' => $term_ids to 'terms' => $term->term_id
    – MightyGas
    Sep 10, 2016 at 16:24
  • It works as is perfectly well on my set up, but anyway, I'm glad you got it to work, maybe answer your own question with the code you ended up using to make it work.. help maybe others
    – Malisa
    Sep 10, 2016 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.