简体   繁体   中英

select query to get results of meta_key

I have a custom field name "front_sl_slider". I want to display all the posts that have custom field "front_sl_slider" value set to 1. also, i want to display, thumbnail, title, and other custom fields data for the posts that i get as result of my query.

$meta_pages = $wpdb->get_results("SELECT *    FROM $wpdb->posts WHERE  post_type = 'page' AND  meta_key = 'front_sl_slider' AND meta_value = 1 ORDER BY menu_order", 'OBJECT'); 
    if ( $meta_pages ) 
        {
        foreach ( $meta_pages as $pageChild1 ) : setup_postdata( $pageChild1 );
         echo get_post_meta($pageChild1->ID, 'front_sl_slider', true);
         echo get_post_meta($pageChild1->ID, 'project_client_name', true) ."<br>";
         echo get_post_meta($pageChild1->ID, 'project_architect', true) ."<br>"; 
        endforeach; 
        }
$querystr = "   SELECT $wpdb->posts.* , $wpdb->postmeta.*
                        FROM $wpdb->posts, $wpdb->postmeta
                        WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id 
                        AND $wpdb->postmeta.meta_key = 'Featured_Projects_Page' 
                        AND $wpdb->postmeta.meta_value = 1 
                        AND $wpdb->posts.post_type = 'page'
                        ORDER BY $wpdb->posts.post_date DESC
                     ";
$pageposts = $wpdb->get_results($querystr, OBJECT);

try this, this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM