Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Get page taxonomy term to use in other queries on same page WordPress

  • SOLVED

I've been stumbling around trying to figure out how to get the term from a specific taxonomy of the current page so that I can subsequently populate queries on the page for other post types that share the same term.

Basically:

Page has taxonomy (issues) with term (education policy)

page.php has four parts:
1. standard loop that outputs the page, its sidebar, and such
2. loop for events that have taxonomy term - education policy
3. loop for reports that have taxonomy term - education policy
4. loop for people that have taxonomy term - education policy

I did page specific templates where I could just hardcode the term into the extra loop queries, but I need to know how to do it dynamically (what was originally supposed to be four or five pages is now forty or fifty).

I've found a few similar questions on StackExchange, but none that I could really find my way through implementing.

I hope this makes sense and many thanks.

Here's the code from page.php:



<?php get_header(); ?>

<div class="content-main grid9">

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

<article id="post-<?php the_ID(); ?>">
<header>
<h1><?php the_title(); ?></h1>
</header>

<div <?php post_class('grid6 first'); ?>>
<?php the_content(); ?>

<?php wp_link_pages( array( 'before' => '<nav>' . __( 'Pages:', 'onntheme' ), 'after' => '</nav>' ) ); ?>

<footer>
</footer>

<? if ( comments_open() ) { ?>
<?php comments_template( '', true ); ?>
<? } ?>

</div>

</article>

<?php endwhile; ?>

<?php get_sidebar(); ?>

<div class="clearfix"></div>

<section class="content-footer">

<ul class="grid3 first">

<header>Reports</header>

<?php $reportsloop = new WP_Query( array( 'Issues' => 'bill-122', 'post_type' => 'onn_reports', 'posts_per_page' => 5 ) ); ?>
<?php while ( $reportsloop->have_posts() ) : $reportsloop->the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
<li>
<header>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
</header>

<div class="postmeta date"><?php the_field('report_author'); ?></div>

<div class="postmeta location"><?php the_field('report_type'); ?></div>

</li>
<?php endwhile; ?>
</ul>

<ul class="grid3">

<header>News</header>

<?php $newsloop = new WP_Query( array( 'Issues' => 'bill-122', 'posts_per_page' => 5 ) ); ?>
<?php while ( $newsloop->have_posts() ) : $newsloop->the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
<li>
<header>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
</header>

<?php the_excerpt(); ?>

</li>
<?php endwhile; ?>
</ul>

<ul class="grid3">

<header>Events</header>

<?php $eventsloop = new WP_Query( array( 'Issues' => 'bill-122', 'post_type' => 'onn_events', 'posts_per_page' => 5 ) ); ?>
<?php while ( $eventsloop->have_posts() ) : $eventsloop->the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
<li>
<header>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
</header>

<?php if(get_post_meta($post->ID, 'ecpt_eventcost', true)): ?>
<div class="postmeta date"><?php echo get_post_meta($post->ID, 'ecpt_eventcost', true); ?></div>
<?php endif; ?>

<?php if(get_post_meta($post->ID, 'ecpt_startdate', true)): ?>
<div class="postmeta location"><?php echo get_post_meta($post->ID, 'ecpt_startdate', true); ?></div>
<?php endif; ?>

</li>
<?php endwhile; ?>
</ul>

</section>

</div>

Answers (2)

2011-06-20

Utkarsh Kukreti answers:

Could you please post the code you used?


Christopher comments:

Added. Thanks for taking a look.


Utkarsh Kukreti comments:

I'm guessing the hardcoded value is this? 'bill-122'? And it's a term of the Issues taxonomy of that page?


Christopher comments:

Correct.


Christopher comments:

And the Issues taxonomy is shared to the other three post types as well.


Utkarsh Kukreti comments:

Add this code before your first custom WP_Query.

global $post;
$term = wp_get_object_terms($post->ID, 'Issues', array('fields' => 'names'));
$term = $term[0];


This should give you the correct taxonomy (var_dump to test if it doesn't work).

Then replace 'bill-122' with $term in the 3 places you've hardcoded it.


Christopher comments:

That didn't return anything in the other queries.

I have to head out for a few hours, but I'll take a look when I get back and check the dump. Didn't expect help quite this fast. :)


Utkarsh Kukreti comments:

Sure. Please post the output of this (inserting after my previous code) when you're back


var_dump($post);
var_dump($term);


Christopher comments:

Back. So, I put it in, but I'm not sure that I even added it correctly because everything just stalls right after that code is inserted. So here's how I added it:


<header>Reports</header>

<?php
global $post;
$term = wp_get_object_terms($post->ID, 'issues', array('fields' => 'names'));
$term = $term[0];
?>

<?php
var_dump($post);
var_dump($term);
?>

<?php $reportsloop = new WP_Query( array( 'issues' => 'Bill 65', 'post_type' => 'onn_reports', 'posts_per_page' => 5 ) ); ?>
<?php while ( $reportsloop->have_posts() ) : $reportsloop->the_post(); if( $post->ID == $do_not_duplicate ) continue; ?>
<li>
<header>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
</header>

<div class="postmeta date"><?php the_field('report_author'); ?></div>

<div class="postmeta location"><?php the_field('report_type'); ?></div>

</li>
<?php endwhile; ?>
</ul>


Should probably check that I added it in the right place before we figure out if it's right or wrong. I also noticed that the tax queries were only working with the names (Bill 65), though it would be preferable to have them working with their slugs.


Utkarsh Kukreti comments:

That code looks fine to me. Are you getting no output at all when you add this?


Christopher comments:

I get the <header>Reports</header> and then nothing after that.


Utkarsh Kukreti comments:

Could you set WP_DEBUG to true in your wp-config.php file, and see the output again?


Christopher comments:

Fatal error: Cannot use object of type WP_Error as array in .../pagetemplate-constellations.php on line 49.


Line 49 is:

$term = $term[0];


Utkarsh Kukreti comments:

Ah. Is "Issues" your taxonomy name? If not, please change it to the correct name, if it is, please change

$term = wp_get_object_terms($post->ID, 'issues', array('fields' => 'names'));

to

$term = wp_get_object_terms($post->ID, 'issues', array('fields' => 'names'));
var_dump($term);


That should print the exact error.


Christopher comments:

'issues' is working fine in the queries below, but judging by this error I guess it must be the problem:

object(WP_Error)#588 (2) { ["errors"]=> array(1) { ["invalid_taxonomy"]=> array(1) { [0]=> string(16) "Invalid Taxonomy" } } ["error_data"]=> array(0) { } }

Something in the way the plugin adds it?


Utkarsh Kukreti comments:

Would it be possible to get credentials to the site you're testing this on? Would be a lot faster that way.


Christopher comments:

Absolutely. Sent via message.

2011-06-20

John Cotton answers:

It depends how you have set up your pages, but the query vars array is worth looking at:

Stick this code on your template page and see what you get:


global $wp;
print_r($wp->query_vars);


JC