Get titles of all custom post types in a page in wordpress

I’m outputting a bunch of custom post types within a page. How do I get all the titles of the posts within this current page?

Related posts

Leave a Reply

2 comments

  1. You should look at WP_Query() for outputting custom post types. The code below gets all of your custom posts of the type ‘custom_post_type’, puts them in a variable called $loop and iterates through it, outputting the title of each post contained within.

    <?php
    // Get the 'Profiles' post type
    $args = array(
        'post_type' => 'custom_post_type',
    );
    $loop = new WP_Query($args);
    
    while($loop->have_posts()): $loop->the_post();
    
    the_title();
    
    endwhile;
    wp_reset_query();
    ?>
    

    There are other arguments that you can pass in to WP_Query() to make it more suitable for your needs. Documentation can be found here.