D
D
Daniil Kuznetsov2018-08-23 12:23:09
WordPress
Daniil Kuznetsov, 2018-08-23 12:23:09

How to organize custom posts?

Good afternoon, I am faced with the task of ordering custom posts of custom categories by the number of user votes. For this I use this code:

<?php query_posts( array ( 'post_type' => 'servers', 'posts_per_page' => 10, 'meta_key' => 'ratings_users', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'tax_query' => array(
    array(
      'taxonomy' => 'serverscat',
      'field'    => 'id',
      'terms'    => 'id'
    )
  ),) ); while ( have_posts() ) : the_post(); ?>
    
    <?php
      /* Include the Post-Format-specific template for the content.
        * If you want to override this in a child theme, then include a file
        * called content-___.php (where ___ is the Post Format name) and that will be used instead.
      */
      get_template_part( 'template-parts/content-servers', get_post_format() );
    ?>
    
    <?php endwhile; ?>
    
    <?php // if WP version 4.1.0 or above use the_posts_pagination() built in function.
    if (4.1 <= floatval(get_bloginfo('version'))):?>                    
    <?php the_posts_pagination( array(
      'mid_size' => 1,
      'prev_text' => __( '&#8249; Предыдущая', 'mmogames' ),
      'next_text' => __( 'Следующая &#8250;', 'mmogames' ),
    ) ); ?>
    <?php else : ?>
    <?php mmogames_pagination(); ?>
    <?php endif; ?>	

    <?php the_archive_description( '<div class="dseotxt">', '</div>' ); ?>
    
    <?php else : ?>	
    <?php get_template_part( 'template-parts/content', 'none' ); ?>		
    <?php endif; ?>

Problem: - how to substitute the id of the current custom category in 'terms' => 'id'.
Thank you in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniil Kuznetsov, 2018-08-23
@Gvynblade

Found a solution

<?php $queried_object = get_queried_object()->term_id;
 $servers1 = new WP_Query( array ( 'post_type' => 'servers', 'posts_per_page' => 10, 'meta_key' => 'ratings_users', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'tax_query' => array(
    array(
      'taxonomy' => 'serverscat',
      'field'    => 'id',
      'terms'    => $queried_object
    )
  ),) ); if ( $servers1->have_posts() ) : ?>

<?php while ( $servers1->have_posts() ) : $servers1->the_post(); ?>
    
    <?php
      /* Include the Post-Format-specific template for the content.
        * If you want to override this in a child theme, then include a file
        * called content-___.php (where ___ is the Post Format name) and that will be used instead.
      */
      get_template_part( 'template-parts/content-servers', get_post_format() );
    ?>
    
    <?php endwhile; ?>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question