Function to return custom post type titles from blog id 1

It’s returning just 1 post title because everytime it reads a post it replaces the content of the var $titles with the title of the current post.

Try this, it will return an array with the post titles.

function getctas() {

    $titles_array = array();
    switch_to_blog(1);

    $args = array(
        'post_type' => 'location_icons'
     );

    $ctas = new WP_Query( $args );
    while ( $ctas->have_posts()) {
        $ctas->the_post();
        array_push($titles_array, get_the_title() );
    }
    restore_current_blog();
    return $titles_array;
}

To add the ID you could do something like this:

  • Create a temporary array to hold the informations you want
  • Add the informations to the array
  • And then push that information in to the output array.
function getctas() {

    $titles_array = array();
    switch_to_blog(1);

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

    $ctas = new WP_Query( $args );
    while ( $ctas->have_posts()) {
        $ctas->the_post();

    $temp_array               = array();
        $temp_array['post_id']    = get_the_ID();
        $temp_array['post_title'] = get_the_title();    

        array_push($titles_array, $temp_array );
    }
    restore_current_blog();
    return $titles_array;
}