Home > other >  Wordpress - Display Custom Post Type by Tag (PHP)
Wordpress - Display Custom Post Type by Tag (PHP)

Time:03-01

I am having issues getting my posts to be shown by the tag-name in Wordpress. I have a Custom Post Type called Reviews with a (taxonomy?) category named Gametypes. Within Gametypes I have a tag named Newest.

I am trying to get posts to display which is tagged by the Newest tag. I have tried the following code which does not work, and I am not sure why:

$args = array( 'tax_query' => array( array( 'taxonomy' => 'Gametypes', 'field' => 'slug', 'terms' => 'newest' ) ) );
$postslist = get_posts( $args ); 

I've tried different iterations of it with and without capital starting letter but I cannot seem to get it to work. Anyone who could shed light upon what I am doing wrong? I am able to pull every post from Reviews (posts with and without tags) with this code:

$args = array( 'post_type' => 'Reviews', 'numberposts' => 6, 'order'=> 'DESC', 'orderby' => 'date' );

In case this information helps. I hope someone can guide me in the right direction!

CodePudding user response:

I found the answer myself, in case anyone wonders!

$args = array(
  'post_type' => 'Reviews',
  'numberposts' => 6,
  'tax_query' => array(
      array(
          'taxonomy' => 'gametypes',
          'field'    => 'slug',
          'terms'    => 'newest',
      ),
  ),
);
$query = new WP_Query( $args );
$postslist = get_posts( $args );

CodePudding user response:

Use this one:

<?php
$query = new WP_Query(
    array( "post_type" => "Reviews", //  "your-post-type" !
           "tag" => "Newest"
    ) );
while ($query->have_posts()) : $query->the_post(); ?>
  
<?php endwhile; ?>
  • Related