logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$20
Get page taxonomy term to use in other queries on same page

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>

This question has been answered.

cwulff | 06/20/11 at 11:02am Edit

Previous versions of this question: 06/20/11 at 11:11am

(2) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/20/11
    11:08am
    Utkarsh Kukreti says:

    Could you please post the code you used?

    • 06/20/11 11:12am

      cwulff says:

      Added. Thanks for taking a look.

    • 06/20/11 11:14am

      Utkarsh Kukreti says:

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

    • 06/20/11 11:20am

      cwulff says:

      Correct.

    • 06/20/11 11:21am

      cwulff says:

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

    • 06/20/11 11:22am

      Utkarsh Kukreti says:

      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.

    • 06/20/11 11:31am

      cwulff says:

      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. :)

    • 06/20/11 11:33am

      Utkarsh Kukreti says:

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


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

    • 06/20/11 3:38pm

      cwulff says:

      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.

    • 06/20/11 3:42pm

      Utkarsh Kukreti says:

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

    • 06/20/11 3:44pm

      cwulff says:

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

    • 06/20/11 3:46pm

      Utkarsh Kukreti says:

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

    • 06/20/11 3:48pm

      cwulff says:

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


      Line 49 is:

      $term = $term[0];

    • 06/20/11 3:52pm

      Utkarsh Kukreti says:

      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.

    • 06/20/11 3:58pm

      cwulff says:

      '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?

    • 06/20/11 4:01pm

      Utkarsh Kukreti says:

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

    • 06/20/11 4:05pm

      cwulff says:

      Absolutely. Sent via message.

  • avatar
    Last edited:
    06/20/11
    11:16am
    John Cotton says:

    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

This question has expired.



cwulff voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.