Log All Post Meta For All Posts

This function will log all post meta for all pages and posts of all types in WordPress.

(Post meta are also known as custom fields or post metadata.)

This will log a list of each post that has any post meta attached to it, and their post meta values. It will be logged in your PHP error log (wherever PHP errors are logged in your server setup).

(See this instead: if you need to find posts that have just one specific custom post meta.)

See below for a tip and usage example.

PHP

function isa_log_all_post_meta() {
     $args = array(
        'post_type'      => array_keys( get_post_types( array('public' => true), 'names' ) ),
        'posts_per_page' => -1,
        'post_status'    => array( 'publish', 'draft', 'future' )
   );
  
    $the_query = new WP_Query( $args );
  
    if ( $the_query->have_posts() ) {
  
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
  
            $value = get_post_meta( get_the_ID() );
              
            if ( $value ) {
                 
                error_log( sprintf( 'Post ID %d (%s): %s', 
                                get_the_ID(),
                                get_the_title(),
                                print_r($value, true)
                ) );
  
            }
        }
  
    }
    wp_reset_postdata();
}

Tip

This logs for all post types, including custom post types, but if you want to limit it to only regular posts and pages, you can change line 3 above to:

'post_type' => array('post', 'page'),

Usage

Use the function like this:

PHP

isa_log_all_post_meta();

It will log each post with its post ID, post title, and an array of all its post meta, like this:

Post ID 518 (THIS POST TITLE): Array
(
    [_edit_last] => Array
        (
            [0] => 1
        )

    [_thumbnail_id] => Array
        (
            [0] => 1018
        )

    [_edit_lock] => Array
        (
            [0] => 1608505027:1
        )

    [custom_field] => Array
        (
            [0] => 
        )

)

See more:

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]