Title: _prime_post_parent_id_caches
Published: November 8, 2023
Last modified: May 20, 2026

---

# _prime_post_parent_id_caches( int[] $ids )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#parameters)
 * [Source](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#wp--skip-link--target)

Prime the cache containing the parent ID of various post objects.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#parameters)󠁿

 `$ids`int[]required

ID list.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#source)󠁿

    ```php
    function _prime_post_parent_id_caches( array $ids ) {
    	global $wpdb;

    	$ids = array_filter( $ids, '_validate_cache_id' );
    	$ids = array_unique( array_map( 'intval', $ids ), SORT_NUMERIC );

    	if ( empty( $ids ) ) {
    		return;
    	}

    	$cache_keys = array();
    	foreach ( $ids as $id ) {
    		$cache_keys[ $id ] = 'post_parent:' . (string) $id;
    	}

    	$cached_data = wp_cache_get_multiple( array_values( $cache_keys ), 'posts' );

    	$non_cached_ids = array();
    	foreach ( $cache_keys as $id => $cache_key ) {
    		if ( false === $cached_data[ $cache_key ] ) {
    			$non_cached_ids[] = $id;
    		}
    	}

    	if ( ! empty( $non_cached_ids ) ) {
    		$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.ID, $wpdb->posts.post_parent FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) );

    		if ( $fresh_posts ) {
    			$post_parent_data = array();
    			foreach ( $fresh_posts as $fresh_post ) {
    				$post_parent_data[ 'post_parent:' . (string) $fresh_post->ID ] = (int) $fresh_post->post_parent;
    			}

    			wp_cache_add_multiple( $post_parent_data, 'posts' );
    		}
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/post.php#L8304)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/post.php#L8304-L8340)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_cache_add_multiple()](https://developer.wordpress.org/reference/functions/wp_cache_add_multiple/)`wp-includes/cache.php` |

Adds multiple values to the cache in one call.

  | 
| [wp_cache_get_multiple()](https://developer.wordpress.org/reference/functions/wp_cache_get_multiple/)`wp-includes/cache.php` |

Retrieves multiple values from the cache in one call.

  | 
| [wpdb::get_results()](https://developer.wordpress.org/reference/classes/wpdb/get_results/)`wp-includes/class-wpdb.php` |

Retrieves an entire SQL result set from the database (i.e., many rows).

  |

[Show 1 more](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#)

| Used by | Description | 
| [WP_Query::get_posts()](https://developer.wordpress.org/reference/classes/wp_query/get_posts/)`wp-includes/class-wp-query.php` |

Retrieves an array of posts based on query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_prime_post_parent_id_caches/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.4.0](https://developer.wordpress.org/reference/since/6.4.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_prime_post_parent_id_caches%2F)
before being able to contribute a note or feedback.