Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I had a issue with a custom taxonomy and a custom post type using the same url and that is now fixed but it has created another bug. not showing the posts tagged with the custom taxonomy.

my url i am using for my post type is /news and then i have a taxonomy which i need to have /news/cars

I can get a all the posts for the custom post type to output on the index template.

I also have the single taxonomy page working but my query doesnt show the posts that are tagged with the custom taxonomy for example all posts that are cars.

here is my code i have so far :

    function register_custom_post_types() {
    $args = array(
        'labels' => array(
            'name' => __( 'News' ),
            'singular_name' => __( 'News' )
        ),
        'menu_position' => 5,
        'public' => true,
    'query_var' => 'news',
        'has_archive' => 'news',
        'hierarchical' => true,
        'taxonomies' => array('news_categories'),
    'publicly_queryable' => true,
        'capability_type' => 'post',
        'rewrite' => array('with_front' => false, 'slug' => 'news/%news_categories%'),
        'supports' => array('title', 'editor', 'thumbnail', 'revisions', 'page-attributes')
    );
    register_post_type( 'news', $args);

}
add_action( 'init', 'register_custom_post_types' );

add_action( 'init', 'create_my_taxonomies', 0 );

    function create_my_taxonomies() {
        register_taxonomy(
            'news_categories',
            'news',
            array(
                'labels' => array(
                    'name' => 'News Categories',
                    'add_new_item' => 'Add New Category',
                    'new_item_name' => "New Category"
                ),
                'query_var' => true,
                'show_ui' => true,
                'show_tagcloud' => false,
                'hierarchical' => true,
                'rewrite' => array(
                    'with_front' => false,
                    'slug' => 'news'
                )
            )
        );
    }

    add_filter('post_type_link', 'news_term_permalink', 10, 4);
    function news_term_permalink($post_link, $post, $leavename, $sample) {
        if ( false !== strpos( $post_link, '%news_categories%' ) ) {
            $glossary_letter = get_the_terms( $post->ID, 'news_categories' );
            $post_link = str_replace( '%news_categories%', array_pop( $glossary_letter )->slug, $post_link );
        }
        return $post_link;
share|improve this question
    
    
I'm not sure if i follow. I can see my single taxonomy post but my query doesn't show posts that are tagged with a certain taxonomy. But I can see all of them on a archive page. –  asaunders Mar 1 at 14:36
    
What code are you using for the query? –  Brad Dalton Mar 2 at 3:52
    
Hi I have edited my question to show you what my query looks like. I would like to point out i am using the index.php template to do the blog posts, the custom post type posts and trying to get it to work on the taxonomy listing pages. I know this should work as wordpress will default back to this template if no template for the taxonomy is found which is what id like –  asaunders Mar 2 at 10:00
    
I have fixed my problem. im not 100% sure but maybe you can correct me but you cant use a id to get a list of taxonomy posts you have to use its name 'term' and that fixed it for me –  asaunders Mar 2 at 17:27

1 Answer 1

up vote 1 down vote accepted

Answer moved from an edit in the answer to a real answer.

This is my query

<?php
                        $taxonomy = (get_post_type() == 'post' ? 'category' : (get_post_type() == 'news' || is_tax('news_categories') ? 'news_categories' : 'category'));

                        $tax_terms = get_terms($taxonomy, array('hide_empty'=> false) );
                        global $wp_query;

                        if( isset($_GET['s']) ):
                        $wp_query->is_search = true;
                    endif;

                    // var_dump($activeCategoryName = $wp_query->queried_object);

                        if(is_tax('news_categories')) {
                                $activeCategoryID = $wp_query->queried_object->term_id;
                                $allLink = get_bloginfo('url') . '/news';
                                $activeCategoryName = $wp_query->queried_object->name;
                        } elseif(is_category() || is_search()) {
                            $activeCategoryID = $wp_query->queried_object->term_id;
                            $activeCategoryName = $wp_query->queried_object->name;
                            $allLink = get_permalink(get_option('page_for_posts'));
                        } elseif (!is_search() && is_home()) {
                            $activeCategoryName = 'All Blog';
                        } elseif(!is_search() && is_post_type_archive('news') ) {
                            $activeCategoryName = 'All News';
                        }

                    ?>
                    <ul>
                        <?php

                        ?>
                        <li><a <?php echo ( !is_search() ? ( is_post_type_archive('news') || is_home() ? 'class="active_category"' : '' ) : '' ); ?> title="View all posts in news" href="<?php echo $allLink; ?>">All</a></li>
                        <?php
                            foreach ($tax_terms as $tax_term) {
                                echo '<li><a ' . ($activeCategoryID == $tax_term->term_id ? 'class="active_category"' : '') . ' title="View all posts in ' . $tax_term->name . '" href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '">' . $tax_term->name . '</a></li>';
                            }
                        ?>
                    </ul>
                </div>
                <div id='search' class='col-lg-3 col-md-3'>
                    <?php get_search_form(); ?>
                </div>
            </div>
        </div>
    </section>
    <div id='blog_listing'>
        <div class="container">
            <div class="row">

                    <!-- <h1><?php echo wp_title('', false); ?></h1> -->
                    <?php
                        if(!is_search()) {
                    ?>
                        <h1>Currently Viewing <?php echo $activeCategoryName; ?> Articles</h1>
                    <?php
                        } else {
                    ?>
                        <h1>Search Results for <?php echo get_search_query(); ?></h1>
                    <?php
                        }
                    ?>
                    <?php
                        $postType = (is_home() ? 'post' : (is_post_type_archive('news') ? 'news' : 'post' ) );

                        $args = array(
                            'posts_per_page' => 6,
                            'post_type' => $postType,

                        );
                        $searchQuery = get_search_query();
                        if(!empty($searchQuery)) {
                            $args['s'] = $searchQuery;
                        };

                        if($postType == 'news') {
                            $args['news_categories'] = $activeCategoryID;
                        } elseif($postType == 'post') {
                            $args['cat'] = $activeCategoryID;
                        }

                        $wp_query = new WP_Query($args);

                        if ($wp_query->have_posts()) : while ($wp_query->have_posts()) : the_post();
                        $date = get_the_date();
                        $published_posts = $wp_query->found_posts;
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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