0

I have a custom post-type call Called 'Sectors' and another post type called 'Challenges' The challenges post type has a taxonomy called 'sectortype' - which has the same names as the sectors.

I created a page called 'single-sector.php' On that page displays a loop that includes challenges related to that sector.

When I write the loop for displaying challenges, how do I make the 'sectortype' => 'advanced-education' a variable so it will work on other single sector pages?

Here's what I have for the loop...

<?php $challenge_args = array(
     'post_type' => 'challenge',
     'sectortype' => 'advanced-education', //Need Help Here
      );
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>

1 Answer 1

2

Get Custom posts by custom taxonomy terms :

<?php
   $terms = get_terms('sectortype');
   $challenge_args = array(
   'post_type' => 'challenge',
   'publish_status' => 'published',
   'posts_per_page' => -1,
   'tax_query' => array(
      array(
        'taxonomy' => 'sectortype',
        'field'    => 'slug',
        'terms'    => $terms[0], //whichever term you want to select
      ),
    ),
);
// create a new instance of WP_Query
$challenge_query = new WP_Query( $challenge_args );
?>
<?php if ( $challenge_query->have_posts() ) : while ($challenge_query->have_posts() ) : $challenge_query->the_post(); // run the loop ?>

DISPLAY IN SEPARATE PAGES TO display the posts in separate pages as you mentioned in the comment, you have to do the following:

Create Separate Page Links:: (use on page as navigation items)

<?php $categories = get_terms('sectortype');?>
<ul>
    <?php foreach( $categories as $key => $c ):?>
      <?php $cat_link = get_term_link( $c->term_id );?>
      <?php $term_title= single_term_title('', false);?>
      <li class="<?php echo ($c->name == $term_title )?'active':'';?>"><a href="<?php echo $cat_link;?>"><?php echo $c->name;?></a></li>
      <?php endforeach;?>
 </ul>

Create a file in theme directory (actually an archive template for taxonomy terms) with the filename 'taxonomy-sectortype.php'.

On that template, get the posts from the usual loop without using any queries and you will get the respective posts.

Sign up to request clarification or add additional context in comments.

1 Comment

For the taxonomy, I have categories such as business, advanced-education etc. So with the edits you provided (thank you) that should display posts from a given taxonomy on the correct page without having to create a new page for each one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.