'How can I get all the posts of a category in Wordpress

I am trying to get in PHP using wordpress, to be able to list my posts that contain the name of my category

Example: I have the category fruit and vegetables And within those categories I have an article about Best Vegetables and Best Fruits, and I would like to print the name of the category, but that the link will take me to the post about the best fruit or vegetables.

I only managed to get the categories.

foreach($categories as $category) {
   echo '<div class="col-md-4"><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></div>';
}```



Solution 1:[1]

You can use 'category_name' in parameters. get_posts() docs

// Get posts by category name
    $posts = get_posts( [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'category_name'  => 'csharp',
    ] );
    // Get posts by category id
    $posts = get_posts( [
        'post_type'      => 'post',
        'post_status'    => 'publish',
        'category'       => '1,2,3',
    ] );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Siavash