looneydoodle
looneydoodle

Reputation: 369

wordpress custom query with meta data

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);

Upvotes: 0

Views: 1347

Answers (1)

janw
janw

Reputation: 6662

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

Upvotes: 1

Related Questions