Search code examples
wordpresscustom-post-type

Display one custom post type at 3 different home page location based on categories


```$specialmenuItems = new WP_Query(array( 
    'post_type'         => 'special_menu',
    'posts_per_page'    => -1,
));```

this is my custom post type (special menu), in the admin area there are three categories for this post type (dinner, drink and lunch). Main question is how can we display this post type at different locations based on different categories ?


Solution

  • You need to add the category (it is Taxonomy) to the WP_Query:

    $specialmenuItems = new WP_Query( array(
        'post_type' => 'special_menu',
        'tax_query' => array(
            array (
                // Here is the taxonomy id
                'taxonomy' => 'category',
                'field' => 'slug',
                // Here you put needed category slug
                'terms' => 'dinner',
            )
        ),
    ) );
    
    while ( $specialmenuItems->have_posts() ) :
        $specialmenuItems->the_post();
        // Show Posts ...
    endwhile;
    
    wp_reset_postdata();