Support » Fixing WordPress » How to get all custom fields values from ALL posts

  • Resolved kg69design

    (@kg69design)


    Each post have 2 custom fields:
    key_1 = value_1
    key_2 = value_2
    How to get all these custom fields values from ALL posts that have post_status = ‘publish’?

    I try something like that:

    global $post;
    global $wpdb;
    $sql = "SELECT * FROM $wpdb->postmeta
    				LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.post_status = $wpdb->postmeta.post_status)
    				WHERE meta_key = 'key_1' OR meta_key = 'key_2'
    				AND post_status = 'publish'
    				ORDER BY post_id ASC";
    
    $custom_fields = $wpdb->get_results($sql);
    
    echo '<pre>';
    print_r ($custom_fields);
    echo '</pre>';

    But it didn’t work. Just an ampty arrow. Can anybody help?

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to get all custom fields values from ALL posts’ is closed to new replies.