Question

I don't if it's possible or not but I have two arrays. The array one is something like this:

     Array ( [0] => link
             [1] => link
             [2] => link 
             [3] => link

I have another array:

Array ( [0] => WP_Post Object ( [ID] => 83 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [1] => WP_Post Object ( [ID] => 33 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [2] => WP_Post Object ( [ID] => 34 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45)
        [3] => WP_Post Object ( [ID] => 80 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45) )

Is it possible to add each of the value of first array into second array with a specific key: For example:

Array (     [0] => WP_Post Object ( [ID] => 83 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [1] => WP_Post Object ( [ID] => 33 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [2] => WP_Post Object ( [ID] => 34 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link)
            [3] => WP_Post Object ( [ID] => 80 [post_author] => 1 [post_date] => 2014-03-05 16:30:45 [post_date_gmt] => 2014-03-05 16:30:45 [post_permalink] => link) )

I tried array_merge but that made two sub arrays into an array. Is there a way I can achieve the above result. Any help would be highly appreciated. Thanks

My php code for arrays:

<?php 
     $postids = array();
     $value = array();
     if ( $the_query->have_posts() ) { ?>

     while ($the_query->have_posts()) : $the_query->the_post(); 

     $postids[]=$the_query->post; 

     $value[] = get_permalink();

    ?>


    <?php endwhile; ?>
Was it helpful?

Solution

You should be able to just add it to the post object before you put it into the $postids array:

$postids = array();
if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
        $the_query->the_post();
        $the_query->post->post_permalink = get_permalink();
        $postids[] = $the_query->post;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top