M
M
Mazino2018-05-11 13:31:18
WordPress
Mazino, 2018-05-11 13:31:18

How to display custom taxonomy with custom post type via main query?

I created an arbitrary taxonomy taxnews , as well as an arbitrary post type news , after which I transferred the corresponding posts from the standard category to this tax ( the category where they were also fixed ) and changed their type to news , but I can’t make it output by standard means. In theory, everything should be the default. For example, in my template, the output of posts goes through the category.php file , I copied the code from there to the new taxonomy-taxnews.php , by the way, by default, the output of the new taxonomy was done through archive.php (maybe this is important). In any case, regardless of the output file, 404 is returned.
Cured like this:

$qur = 'post_type=news';
query_posts( $qur );

However, pagination did not work this way, all subsequent pages after the initial output were empty. And although that too can be fixed. How all the same to deduce by the main request which takes parameters from the link? Parameter taxnews=allnews, parameter for standard taxonomy category_name=allnews.
I did it based on this article , the code is identical except for the output files ( I have a different template ).
Part of the code from the output file responsible for displaying and paginating posts:
sheet
<?php 
/**
 * Examiner taxnews page
**/ 
?>
<?php get_header();
$exm1_category_post_style = get_option('exm1_category_post_style');
$author_show  = get_option('exm1_category_show_author');
$date_show  = get_option('exm1_category_show_date');

?>

  <?php $exm1_popular_widget = get_option('exm1_popular_widget'); if ($exm1_popular_widget == "true") { ?>
  <div class="fullwidth popular-part">
    <div class="home-widget four-parts">
      <?php exm1_popular_posts();?>
    </div>
    <!--home-widget four-parts-->
  </div>
  <!--fullwidth-->
  <?php } ?>

<div id="main">
  <div id="primary" class="three-parts archive">
    <?php if ($exm1_popular_widget != "true") {?>
    <div class="widget-title">
      <h1>
        <?php echo esc_html(get_option('exm1_word_before_category')); ?>
        <?php single_cat_title(); ?>
      </h1>
    </div>
    <!--widget-title-->
    <?php } ?>
    <div id="blog-list" <?php if ( $exm1_category_post_style == 'style_1' ){echo 'class="blog-category"';}elseif( $exm1_category_post_style == 'style_2' ){echo 'class="exm1-blog-posts-category"';}elseif( $exm1_category_post_style == 'style_3' ){echo 'class="img-featured"';}?>>
      <ul>		
      <?php if (have_posts()) : while (have_posts()) : the_post();?>								
        <li>					
        <?php if ( $exm1_category_post_style == 'style_1' ){	
          category_post_style1($author_show, $date_show);	
          }elseif( $exm1_category_post_style == 'style_2' ){
          category_post_style2($author_show, $date_show);
          }elseif( $exm1_category_post_style == 'style_3' ){
          category_post_style3($author_show, $date_show);	
          }?>			
        </li>
        <?php endwhile; else : ?>
          <div class="widget-title"><?php echo esc_html(get_option('exm1_no_match')); ?></div>
        <?php endif;?>
      </ul>
    </div>
    <?php $exm1_pagination_style = get_option('exm1_pagination_style');if($exm1_pagination_style =='ajax' || $exm1_pagination_style =='auto-load' ){?>
    <div class="pagination pagination-load-more <?php if($exm1_pagination_style =='auto-load'){echo esc_attr('auto-load');}?>">
      <?php $loadmoreword = get_option('exm1_word_load_more');
      next_posts_link(esc_html($loadmoreword), '' ); ?>
    </div>
    <!--pagination-->
    <?php } else { ?>
    <div class="pagination-simple">
      <?php exm1_pagination(); ?>
    </div>
    <!--pagination-simple-->
    <?php } ?>
  </div>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Max Medar, 2018-05-11
@MedVedar

1. Forget about query_posts()
2. I ripped out a piece of code from taxonomy-product-category.php, which displays custom posts from a custom taxonomy with working pagination:

$term      = get_queried_object();
          $term_slug = $term->slug;
          $paged     = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
          $_posts    = new WP_Query( array(
            'post_type'      => 'product',
            'posts_per_page' => 10,
            'paged'          => $paged,
            'tax_query'      => array(
              array(
                'taxonomy' => 'product-category',
                'field'    => 'slug',
                'terms'    => $term_slug,
              ),
            ),
          ) );
          global $wp_query;
          $tmp_query = $wp_query;
          $wp_query  = null;
          $wp_query  = $_posts;
          if ( $_posts->have_posts() ) :
            while ( $_posts->have_posts() ) :
              $_posts->the_post();
              get_template_part( 'template-parts/content-product', 'preview' );
            endwhile;
            the_posts_pagination( array(
              'type' => 'list',
              'prev_text'    => '<i class="fas fa-angle-double-left"></i>',
              'next_text'    => '<i class="fas fa-angle-double-right"></i>',
            ) );
          else :
            get_template_part( 'template-parts/content', 'none' );
          endif;
          $wp_query = null;
          $wp_query = $tmp_query;
          wp_reset_postdata();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question