WP Query Loop


Hi,

I am interested to know how to loop out a WP_Query in beans.

I have made a php file like page-gallery.php in the Beans child theme directory and I put in:

add_action( 'beans_loop_query_args[_gallery]', 'custom_loopy_stuff' );

function custom_loopy_stuff() {

    return array(
        'post_type' => 'my_photos',
    );

}

But now I don't know what function to create next so as to loop out fields in either a foreach or while loop in Beans?

Something like ->have_posts()

Loop custom field and custom field and custom field.

Thank you and all the best.


Hi Joshua,

If you are going to user lots of custom HTML including custom meta, the easiest my be to replace the main loop with your own custom loop and HTML, here is an example snippet:

beans_modify_action_callback( 'beans_loop_template', 'example_content' );
/**
 * Page content.
 */
function example_content() {
 // Set your query.
  $query = new WP_Query( array(
   'post_type' => 'my_photos',
   // ...
  ) );

  while ( $query->have_posts() ) : $query->the_post();
    ?><!-- Post HTML --><?php
 endwhile;

 wp_reset_postdata();
}

If you want to keep the original, I would advise to change the main query using the pre_get_posts action and then inject your post meta using Beans HTML API actions ;-)

Happy coding,


Thank you, Thierry, that is what I needed.

Write a reply

Login or register to write a reply, it's free!