0

My posts contain a meta-field that holds an external id. I'm trying to create a function with a loop that goes through all posts and puts all the meta-keys in an array and returns this array.

This is what i came up with, but it seems i'm missing something. Does anyone have a clue?

function gather_ids ()
{

    $args = array(
            'post_type' => 'post',
    );
        $posts = new WP_Query( $args );

        // The Loop
        if ( $posts->have_posts() ) {

            while ( $posts->have_posts() ) {
                $temp[] = get_post_meta($post_id, 'json_id');
            }
            return $temp;
        } else {
        // no posts found
        echo "no posts found to delete";
        }
    /* Restore original Post Data */
    wp_reset_postdata();
}
2

Your function is unreliable and totally overboard and really really expensive. Furthermore, as already stated by @MarkKaplun, you are not calling the_post() which causes the $post global not to be updated to the current post being looped through, so the value from get_post_meta() will always have the same value.

Although $post_id might work, it is one of those crappy variables set globally that is actually used to get the comments. It is better to use get_the_ID() or even $post->ID as you are inside the loop. For extra info, read this post

To solve your issue, I would just create a function with a custom SQL query to fetch all unique values from a specific meta key. Here is a function I have used on another answer

/**    
 * Description: Getting all the values associated with a specific custom post meta key, across all posts
 * Author: Chinmoy Paul
 * Author URL: http://pwdtechnology.com
 *
 * @param string $key Post Meta Key.
 *
 * @param string $type Post Type. Default is post. You can pass custom post type here.
 *
 * @param string $status Post Status like Publish, draft, future etc. default is publish
 *
 * @return array
 */
function get_unique_post_meta_values( $key = '', $type = 'post', $status = 'publish' ) 
{
    global $wpdb;
    if( empty( $key ) )
        return;
    $res = $wpdb->get_col( 
        $wpdb->prepare( 
            "SELECT DISTINCT pm.meta_value 
            FROM {$wpdb->postmeta} pm
            LEFT JOIN {$wpdb->posts} p 
            ON p.ID = pm.post_id
            WHERE pm.meta_key = '%s'
            AND p.post_status = '%s'
            AND p.post_type = '%s'", 
            $key, 
            $status, 
            $type 
        ) 
    );
    return $res;
}

You can then just use it as follow to get an array of unique meta value

$unique_values = get_unique_post_meta_values( 'json_id' );
?><pre><?php var_dump( $unique_values ); ?></pre><?php  

You can also build in some cache/transient system into the function to optimize it even more

0

Please pass the 3rd argument true.

like

get_post_meta($post_id, 'json_id', true);
  • Thanks, i did try that as well, but I still cant get the array – Bram_Boterham Oct 1 '15 at 9:32
  • I think the problem with time of storing when you call function add_post_meta($post_id, 'json_id', $value) then please check the type of variable is array or other – Mitul Oct 1 '15 at 9:36
0

You don't set the $post_id to anything. You probably need to call the_post() at the loop and use the $post global to access the id.

  • $post_id is one of those (crappy) unknown globals that is suppose to hold the current post ID. Check this answer – Pieter Goosen Oct 2 '15 at 4:56
  • @PieterGoosen, that answer actually says "$post_id is a variable name used commonly to refer the post ID, but it isn't a Core variable..." but even if it is in the context, without declaring it as a global and "moving" to the next post I assume it will not get updated.... – Mark Kaplun Oct 2 '15 at 5:21
  • It does work though, but I do not use it, as I said, it is crappy and is suppose to hold the post ID – Pieter Goosen Oct 2 '15 at 5:24

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.