1

I have the following code in my functions.php file:-

function featured_properties_func( $atts ) {

    $args = array(
        'posts_per_page'=> -1,
        'post_type'     => 'properties',

    );
    $featured_query = new WP_Query( $args );

        if( $featured_query->have_posts() ):

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

                $featured_properties = get_the_title();

                return $featured_properties;

            endwhile;

        endif; wp_reset_query();

    }
add_shortcode( 'featured_properties', 'featured_properties_func' );

When I output the shortcode I'm only getting one value where as it should be returning 6.

What I want to do is loop all the properties and return the title of each, any ideas what I am doing wrong?

5

You are returning inside your loop - so it returns on the first iteration, giving you one result only.

You should build a string inside your loop instead, and only return when the loop is over.

Something like

$featured_properties = '';

      if( $featured_query->have_posts() ):

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

                $featured_properties .= get_the_title() . '<br />';

            endwhile;

        endif; wp_reset_query();

 return $featured_properties;

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.