Fetch the post from wordpress based on decending order date

How to fetch the post from wordpress based on decending order date and meta key=’test’ and meta_value=123

To fetch posts from WordPress based on descending order of date and with a specific custom field (meta key) and its value, you would typically use the WP_Query class. Here’s an example of how you can achieve this:

$args = array(
    'post_type'      => 'post', // or your custom post type
    'posts_per_page' => -1,     // -1 to retrieve all posts
    'meta_key'       => 'test', // your custom field key
    'meta_value'     => 123,    // your custom field value
    'orderby'        => 'date', // order by date
    'order'          => 'DESC', // descending order
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // The post content or other information
        the_title();
        the_content();
        // ... other post-related functions
    }

    // Reset post data
    wp_reset_postdata();
} else {
    // No posts found
    echo 'No posts found';
}

In this example:

  • 'post_type' specifies the post type you want to query. You can change it to a custom post type if needed.
  • 'posts_per_page' is set to -1 to retrieve all posts. You can adjust this based on your requirements.
  • 'meta_key' and 'meta_value' specify the custom field key and its value, respectively.
  • 'orderby' => 'date' orders the results by date.
  • 'order' => 'DESC' orders them in descending order.

Make sure to include this code in a WordPress theme file or in a custom plugin. Adjust the parameters according to your specific needs.

use with meta_query

Certainly! If you want to use meta_query to handle multiple custom fields or more complex conditions, you can modify the query accordingly. Here’s an example using meta_query:

$args = array(
    'post_type'      => 'post', // or your custom post type
    'posts_per_page' => -1,     // -1 to retrieve all posts
    'orderby'        => 'date', // order by date
    'order'          => 'DESC', // descending order
    'meta_query'     => array(
        array(
            'key'     => 'test',
            'value'   => 123,
            'compare' => '=',
        ),
        // Add more meta queries as needed
    ),
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // The post content or other information
        the_title();
        the_content();
        // ... other post-related functions
    }

    // Reset post data
    wp_reset_postdata();
} else {
    // No posts found
    echo 'No posts found';
}

In this example, the meta_query parameter is an array of arrays, each representing a meta query. You can add more arrays to the meta_query array to include additional conditions.

For example, if you want posts where ‘meta_key_1’ equals ‘value_1’ and ‘meta_key_2’ equals ‘value_2’, you would add another array to the meta_query array:

$args = array(
    'post_type'      => 'post', // or your custom post type
    'posts_per_page' => -1,     // -1 to retrieve all posts
    'orderby'        => 'date', // order by date
    'order'          => 'DESC', // descending order
    'meta_query'     => array(
        'relation' => 'AND', // Use 'AND' or 'OR' as needed
        array(
            'key'     => 'test',
            'value'   => 123,
            'compare' => '=',
        ),
        // Add more meta queries as needed
    ),
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // The post content or other information
        the_title();
        the_content();
        // ... other post-related functions
    }

    // Reset post data
    wp_reset_postdata();
} else {
    // No posts found
    echo 'No posts found';
}