|
<?php |
|
// IN TEST MODE. UNCOMMENT update_field() BELOW TO RUN. |
|
/* Get posts sorted by ID */ |
|
$args_posts = array( |
|
'post_type' => array( 'post', 'page' ), // Or 'any'. |
|
'fields' => 'ids', |
|
'posts_per_page' => -1, |
|
'orderby' => 'ID', |
|
'order' => 'ASC', |
|
); |
|
$query_posts = new WP_Query( $args_posts ); |
|
|
|
/* Copy MRP array of IDs into ACF field, check for success and log */ |
|
if ( $query_posts->have_posts() ) { |
|
echo '<table class="tabular" style="width: 50%;">'; |
|
while ( $query_posts->have_posts() ) { |
|
$query_posts->the_post(); |
|
$post_id = get_the_ID(); |
|
$post_type = get_post_type(); |
|
|
|
if ( MRP_get_related_posts( $post_id ) ) { |
|
// Get array of MRP IDs. |
|
$post_rel_mrp_arr = array_keys( MRP_get_related_posts( $post_id ) ); |
|
// Copy MPR IDs into ACF field, returns post_meta ID. |
|
$update_field = '--'; |
|
// UNCOMMENT To RUN: |
|
// $update_field = update_field( 'related_posts', $post_rel_mrp_arr, $post_id ); |
|
|
|
// Log post meta data into HTML table, with check for success. |
|
$post_rel_mrp_ids = ( is_array( $post_rel_mrp_arr ) ) ? implode( ',', $post_rel_mrp_arr ) : '--'; |
|
$post_rel_acf_arr = get_post_meta( $post_id, 'related_posts' ); |
|
$post_rel_acf_ids = ( is_array( $post_rel_acf_arr[0] ) ) ? implode( ',', $post_rel_acf_arr[0] ) : '--'; |
|
// Test for matched IDs in MRP and ACF fields. |
|
$copy_ids_check = ( $post_rel_mrp_ids === $post_rel_acf_ids ) ? 1 : 0; |
|
?> |
|
<tr><td><?php echo $post_type; ?></td><td><a href="/?p=<?php echo $post_id; ?>"><?php echo $post_id; ?></a></td><td><?php echo count( $post_rel_mrp_arr ); ?></td><td><?php echo $post_rel_mrp_ids; ?></td><td><?php echo $post_rel_acf_ids; ?></td><td><?php echo $copy_ids_check; ?></td><td><?php echo $update_field; ?></td></tr> |
|
<?php |
|
} else { // No MRP IDs. |
|
?> |
|
<tr><td><?php echo $post_type; ?></td><td><a href="/?p=<?php echo $post_id; ?>"><?php echo $post_id; ?></a></td><td>0</td><td></td><td></td><td></td><td></td></tr> |
|
<?php |
|
} |
|
} |
|
echo '</table>'; |
|
} else { |
|
echo 'No posts found.'; |
|
} |
|
|
|
wp_reset_postdata(); |
|
|
|
/* |
|
|
|
MRP IDs |
|
print_r( array_keys( MRP_get_related_posts( $post_id ) ) ); |
|
Array |
|
( |
|
[0] => 7108 |
|
[1] => 9251 |
|
[2] => 10546 |
|
[3] => 10797 |
|
) |
|
|
|
Copied to ACF field: |
|
print_r( get_post_meta( $post_id , 'related_posts' ) ); |
|
Array |
|
( |
|
[0] => Array |
|
( |
|
[0] => 7108 |
|
[1] => 9251 |
|
[2] => 10546 |
|
[3] => 10797 |
|
) |
|
|
|
) |
|
|
|
Serialized: |
|
print_r( get_post_custom( $post_id ] ) ); |
|
[related_posts] => Array |
|
( |
|
[0] => a:4:{i:0;s:4:"1234";i:1;s:4:"5678";i:2;s:5:"12345";i:3;s:5:"67890";} |
|
) |
|
|
|
*/ |
|
?> |