php - How to display all posts assigned the same category?

I have a product category called Cable, and I want to grab all posts assigned to this category and output the title of said posts. This category is from the custom taxonomy 'Category' that woo-commerce adds, I'm not sure if that makes things harder? but I haven't found a solution yet.

Could anyone assist with this?

enter image description here

Answer

Solution:

Try this code:

And modify as per your requirements:

add_shortcode( 'specific_category_posts', 'kb_get_posts' );
function kb_get_posts() {

    $query = new WP_Query(
    array(
        'post_type'      => 'product',
        'post_status'    => 'publish',
        'posts_per_page' => -1,
        'tax_query'      => array(
            array(
                'taxonomy' => 'product_cat',
                'field'    => 'term_id',
                'terms'    => array( '18' ), //Your custom category id
            ),
        ),
    )
);

if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        echo '<h2>' . get_the_title() . '</h2>';
    }
}
wp_reset_postdata();
}

Answer

Solution:

Use this code to show all posts assigned to the category cable

<?php
    global $post;
    $args = array( 'numberposts' => 10, 'category_name' => 'cable' );
    $posts = get_posts( $args );
    foreach( $posts as $post ): setup_postdata($post); 
?>

<div> 
    <?php 
    the_title(); 
    the_excerpt(); ?>
</div>

<?php endforeach; ?>

Answer

Solution:

I have created with list

add_shortcode( 'pi_cable_posts', 'pi_posts' );
function pi_posts() {
$pi_query = new WP_Query(
array(
    'post_type'      => 'product',
    'post_status'    => 'publish',
    'posts_per_page' => -1, //--1 for unlimited and number to limit
    'tax_query'      => array(
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'term_id',
            'terms'    => array( 'YOUR ID HERE' ), //ADD YOUR ID HERE
        ),
    ),
));

if ( $pi_query ->have_posts() ) {
echo '<ul>';
while ( $pi_query ->have_posts() ) {
    $pi_query ->the_post();
    echo '<li><h2><a href="'.get_the_permalink().'">' . get_the_title() . '</h2></li>';
}
echo '</ul>';
}
wp_reset_postdata();}

Answer

Solution:

You can use with tax_query().

EDIT:

Try out this code to get products on category page.

add_shortcode('product_list', 'zillion_show_products_title_by_cat', 10);
function zillion_show_products_title_by_cat()
{
    if(!is_product_category()){
        return;
    }
    $term = get_queried_object();
    
    ob_start();
    $args = array(
        'post_type' => 'product',
        'post_status' => 'publish',
        'tax_query' => array(
             'taxonomy' => 'product_cat',
             'field'    => 'term_id',
             'terms'     =>  $term->term_id, // 21 is category id When you have more term_id's seperate them by comma.
             'operator'  => 'IN'
        )
    );
    $the_query = new WP_Query($args);

    // The Loop
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            echo '<h3>' . get_the_title() . '</h3>';
        }
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();

    $contents = ob_get_clean();
    return $contents;
}

Source