Custom Post Type Queries in WordPress: Fetch Custom Posts Data
In WordPress, Custom Post Type (CPT) is used to define a new type of content beyond the default posts and pages. When querying custom posts, you use the WP_Query class to create a custom query loop.
Below is an example of how you can use arguments (args) in a custom post type loop:
<?php
$args = array(
'post_type' => 'events', // Replace with your custom post type slug
'posts_per_page' => -1, // Number of posts to display
'orderby' => 'date', // Order by date
'order' => 'DESC', // Order in descending order
'post_status' => 'publish', // Show only published posts
);
$query = new WP_Query($args);
?>
<?php
if($query->have_posts()):
while($query->have_posts()): $query->the_post();
?>
<div class="col-md-4">
<div class="events-b-main">
<div class="events-b-m-image">
<img src="<?php echo $url; ?>/assets/images/about/1.png" />
</div>
<div class="events-b-m">
<div class="events-b-m-date">
<span><img src="<?php echo $url; ?>/assets/images/icon/calender.png" > 20 -21 Dec, 2024</span>
</div>
<div class="events-b-m-heading">
<h3><?php the_title(); ?></h3>
</div>
<div class="events-b-m-loc">
<img src="<?php echo $url; ?>/assets/images/icon/location.png" > New Delhi
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata(); // Reset the global post object
else :
echo '<p>No posts found.</p>';
endif;
?>
Frontend & Backend Developer | WordPress | PHP
3 个月Thanks all for liking my post