WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get all custom fields values from ALL posts (4 posts)

  1. kg69design
    Member
    Posted 2 years ago #

    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?

  2. esmi
    Forum Moderator
    Posted 2 years ago #

  3. kg69design
    Member
    Posted 2 years ago #

    Thanks esmi, but I need to get custom fields values, not posts...

  4. kg69design
    Member
    Posted 2 years ago #

    Solved the problem. We can get all custom fields through the loop. Thanks to esmi for the idea.

Topic Closed

This topic has been closed to new replies.

About this Topic