Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save celticwebdesign/ae355a0661c3226b79f88962e927066a to your computer and use it in GitHub Desktop.
Save celticwebdesign/ae355a0661c3226b79f88962e927066a to your computer and use it in GitHub Desktop.
WordPress loop objects into array
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'posts_per_page' => -1
);
$child_query = new WP_Query($args);
$brands_array = [];
if( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post();
$post_terms = wp_get_post_terms( get_the_id(), 'brands' );
$object = new stdClass();
$object->name = $post_terms[0]->name;
$object->slug = $post_terms[0]->slug;
array_push($brands_array,$object);
endwhile; endif;
wp_reset_postdata();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment