0

I am trying to get the latest post with a meta value of headline, but it gives me the latest post made instead. Here is my query, what am i doing wrong?

$querydetails = "
        SELECT $wpdb->posts.* 
        FROM $wpdb->posts, $wpdb->postmeta
        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id  
        AND $wpdb->postmeta.meta_value = 'headline'
        AND $wpdb->postmeta.meta_key = 'custom_select'
        AND $wpdb->posts.post_status = 'publish'
        AND $wpdb->posts.post_type = 'post'
        ORDER BY $wpdb->posts.post_date DESC
        LIMIT 1
    ";

    $headline = $wpdb->get_results($querydetails, OBJECT);

1 Answer 1

1

You should do it in wp_query,
The code below should do it.

<?php
$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'meta_key' => 'custom_select',
    'meta_value' => 'headline',
    'posts_per_page' => '1', //limit
    'paged' => get_query_var( 'page' ),
    'order' => 'DESC',
    'orderby' => 'date'
);
$query = new WP_Query($args);
?>

This way you can use the default wordpress tags like the_content() in a loop

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! that worked! What is wrong with the way i was doing it?
I'm not sure, but you can check the query WP_query made with echo $query->request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.