0

I have the following query which outputs a list of categories for my custom post type called STORIES.

<?php
$taxonomy = 'story-category';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo '<div class="category-grid-box">
<div class="category-grid-content">' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a> </div>
</div>  ';
}
?>

This outputs a list of links for my categories and works great.

My problem is, I don't know how to write the query on the next page which will list all the posts in that chosen category.

So my query lists the categories... - Apples - Oranges - Bananas

If you click on Apples and go to that page, what query do I use to list all of the STORIES that have the category APPLES?

Any ideas? Can't get any solution to work.

I have the following query, but it lists ALL of the categories and ALL of the posts within them. How can I modify it to just show the posts for the page I am on?

<?php
$custom_terms = get_terms('story-category');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'stories',
'tax_query' => array(
array(
'taxonomy' => 'story-category',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';

while($loop->have_posts()) : $loop->the_post();
echo '<p><a href="'.get_permalink().'">'.get_the_title().'</a></p>';
endwhile;
}
}
?>

2 Answers 2

2

Hope this help:

 $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        array(
           'taxonomy' => 'story-category',
           'field'    => 'slug',
           'terms'    => $term->slug,
        ),
    ),
);
$query = new WP_Query( $args );

Class Reference/WP Query

6
  • That would work if the page you are on is 'bob'. How do I list all of the posts within the category I am already in? So I don't have to rewrite the query for 100's of pages?
    – lowercase
    Jun 17, 2015 at 6:04
  • just added a new query which might help.
    – lowercase
    Jun 17, 2015 at 6:09
  • When you click on apple it will take to the apple taxonomy. page. Create a separate taxonomy template( taxonomy.php ) and put the code in that. That will surely work there!
    – Shrikant D
    Jun 17, 2015 at 6:14
  • this code is already going in taxonomy.php but geting blank results with your code.
    – lowercase
    Jun 17, 2015 at 6:15
  • Can you echo me the category title in your code? It is showing blank as nothing is being output?
    – lowercase
    Jun 17, 2015 at 6:19
1

you can create custom taxonomy template for custom post : LINK

6
  • This outputs ALL categories and their posts. I just want to list the ones for the page I am on. If I am on the APPLES page - i only want to list the APPLES posts. not EVERY post form every category like attached link.
    – lowercase
    Jun 17, 2015 at 6:13
  • have you create template or custom taxonomy template..?
    – vrajesh
    Jun 17, 2015 at 6:17
  • as per your taxonomy what is your file name?
    – vrajesh
    Jun 17, 2015 at 6:19
  • have you add 'has_archive=>true' in creation of custom post type?
    – vrajesh
    Jun 17, 2015 at 6:22
  • Let us continue this discussion in chat.
    – vrajesh
    Jun 17, 2015 at 6:22

Your Answer

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

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