Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress: Display Custom Posts by Custom Category

  • This topic is empty.
Viewing 1 post (of 1 total)
  • Author
    Posts
  • #167129
    taxicss
    Participant

    Hi,
    I’m really confused right now. I’ve been trying to display my custom posts but cannot make it to be grouped by their custom category(taxonomy). I have managed to display them by category but the category is hard-coded. These is what I got at the moment.

    <?php 
    $args = array(
      'post_type' => 'foods',
      'tax_query' => array(
        array(
          'taxonomy' => 'food_categories',
          'field' => 'slug',
          'terms' => 'bread'
        )
      )
    );
    
    $foods = new WP_Query($args);
    if($foods->have_posts()) {
      while($foods->have_posts()) {
        $foods->the_post();
        ?>
    
        <h1><?php the_title(); ?></h1>
        <div>
          &lt;?php the_content(); ?&gt;
        </div>
    
        &lt;?php
      }
    }
    
    else {
      echo 'no foods';
    }
    

    ?>

    How can I display all my custom post grouped by their respective “Category” by not hard coding the “category name”?

Viewing 1 post (of 1 total)
  • The forum ‘Back End’ is closed to new topics and replies.