0

How to get the last added meta value by post id

This is what I have tried so far:

    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'picture_upload_1'
    );
    $dbResult = new WP_Query($args);

var_dump($dbResult);

but I am not receiving the meta value

  • you want only 'picture_upload_1' of all post_meta? – Jignesh Patel May 23 '18 at 7:08
  • The last added value for a particular post, or all posts? – Jacob Peattie May 23 '18 at 7:18
-1

Try this code. I will added order by modified by date. Hope is work

https://codex.wordpress.org/Class_Reference/WP_Query

$args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby'     => 'modified',
        'order'       => 'DESC',
        'meta_key' => 'picture_upload_1'
    );
  • This is returning the latest post with that meta value. Dhara is asking for the last added meta value, not the last added post. – Jacob Peattie May 23 '18 at 7:18
  • yes and i don't want single row i want all value which is last added – dhara chauhan May 23 '18 at 7:22
-1

Try this i have edited the rectified what you exactly looking for.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'posts_per_page' => -1,
);

$dbResult = new WP_Query($args);
while ( $dbResult->have_posts() ) {
    $dbResult->the_post();
    global $post;

    // You can pass specific post id to get post meta values 
    $fields = get_post_custom_keys($post->ID);    // all keys for post as values of array
    if ($fields) {
        foreach ($fields as $key => $value) {

            // $value will give you meta_key's
            $meta_value = get_post_meta($post->ID,$value,true);

            // Exclude WP System Meta Keys
            if ($value[0] != '_') { 
                // Meta Value
                echo $meta_value;
            }
        }
    }
}
  • but i want all last added value not only one – dhara chauhan May 23 '18 at 7:04
  • What do you mean all Last added value? – Adeel Abbas May 23 '18 at 7:08
  • if i update and there could be more than one entry in table at one time then i want all latest added meta value by post id – dhara chauhan May 23 '18 at 7:09
  • yes last added or all. explain clear – Jignesh Patel May 23 '18 at 7:10
  • last added there can possibility that there could be more than one row at time – dhara chauhan May 23 '18 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.