Fetch wordpress post and by category id and name

Fetch wordpress post and by category id and name

  • By admin
  • wordpress
  • Comments Off on Fetch wordpress post and by category id and name

Find the below best way to Fetch wordpress post and by category id and name

Fetch post by cat id

$posts = new WP_Query(array(
     'post_type' => 'post',
     'cat' => 11,
     'posts_per_page' => 10, // Adjust the number of posts to display
 ));
 
 while ($posts->have_posts()) {
     $news_posts->the_post();
     
     }

For custom post

'post_type' => 'latest-blog',
       'tax_query' => array(
           array(
               'taxonomy' => 'blog_showcase_cat',
               'field'    => 'slug',
               'terms'    => 'our-blog',
           ),
       ),

list of posts based on various parameters.

$args = array(
        'post_type' => 'post', // Change 'post' to the desired post type
        'posts_per_page' => -1, // Number of posts to retrieve (-1 for all)
        );

        $posts = get_posts($args);

        foreach ($posts as $post) {
        setup_postdata($post);
        // Display post data here
        the_title();
        the_content();
        // You can fetch and display other post data as needed
        }
        wp_reset_postdata();

by category name

<?php
foreach((get_the_category()) as $category) {
  $postcategory= $category->cat_ID;
  $categoryname =$category->cat_name;
  echo $postcategory;
  echo $categoryname;
}
?>

 

Manifest Desires-Click Here