0

I'm trying to create a function to retrieve all titles from a custom post type located on blog id=1.

I tried to use foreach but I cannot get it to work. The closest I have been was using this:

function getctas() {
    switch_to_blog(1);

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

        $ctas = new WP_Query($args);
        while ($ctas -> have_posts()) {
        $ctas -> the_post();
        $titles = get_the_title($post->ID);
        restore_current_blog();
        return $titles;
        }

}

The issue I'm facing with this is that the function is returning only 1 post title and not all as I intend to.

0

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;
}
6
  • Hi Marcos, thanks for your help. I have tried your proposed solution but instead of returning the titles it returns Array
    – Jaypee
    Jun 2 '14 at 18:48
  • Replace 'post_type' => 'post', with your post_type, 'post_type' => 'location_icons' I'll fix it. I have fixed the code, try it now. Jun 2 '14 at 18:51
  • I actually did that when I tried, but I get Array. Maybe because I'm using <?php echo getctas(); ?> on my test template?
    – Jaypee
    Jun 2 '14 at 18:55
  • Yes, I've returned you an array with all the post title, you can't echo an array, you could use a foreach or change the output of the function, instead of an array a string with the titles separated by comas, I don't know how you need it, but with the array you could change to whatever you want it. Jun 2 '14 at 18:57
  • I initially wanted to use this to populate a select on backend, and your code is working there. Thank You!!! PS: If I'd want to add more things to that array, like ID for example, how will be the correct way to do? Thanks Again Marcos!
    – Jaypee
    Jun 2 '14 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.