Code for displaying the latest posts from the 'recipes' post type..

Home » Snippets » Code for displaying the latest posts from the 'recipes' post type..
0

Created with:

Visibility: 

public

Creator: Kushtrim

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
$args = [
    'post_type' => 'recipes',
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
];

$query = new WP_Query( $args );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        // Display post content here
    endwhile;
    wp_reset_postdata();
else :
    // Display message for no posts found
endif;
				

This code creates a new WP_Query object with the specified arguments, which include the post type, number of posts, and sorting order. The code then checks if there are any posts to display using the have_posts() method and iterates through them using the_posts() method. Within the while loop, you can display the post content using template tags such as the_title() and the_content(). After the loop, the code resets the post data using the wp_reset_postdata() function to prevent any conflicts with other WP_Query objects. If no posts are found, the code displays a message.

Note: If the code runs on the frontend, some escaping should be added to the template tags to prevent XSS vulnerabilities.

Register an account to save your snippets or go Pro to get more features.