Custom code for displaying a custom post type in WordPress

Emily T Dec 16, 2024 Custom Post Types
How can I show a specific type of content on my website?
What is the best way to query and display a custom post type in WordPress using WP_Query?
Andy answered Dec 16, 2024

Here's a custom solution for displaying a custom post type in WordPress using WP_Query. You can place this code in your theme's template file (e.g., page.php, single.php, or a custom template).

Basic query to display custom post type posts:

<?php
$args = array(
    'post_type' => 'your_custom_post_type',
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
);

$custom_query = new WP_Query($args);

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry-content">
            <?php the_excerpt(); ?>
        </div>
    </article>
<?php
    endwhile;
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;
?>

For more advanced filtering with taxonomy and custom fields:

<?php
$args = array(
    'post_type' => 'your_custom_post_type',
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            'taxonomy' => 'your_taxonomy',
            'field' => 'slug',
            'terms' => 'your-term'
        )
    ),
    'meta_query' => array(
        array(
            'key' => 'your_custom_field',
            'value' => 'desired_value',
            'compare' => '='
        )
    )
);

$custom_query = new WP_Query($args);

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry-content">
            <?php the_excerpt(); ?>
        </div>
    </article>
<?php
    endwhile;
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;
?>

With pagination support:

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'post_type' => 'your_custom_post_type',
    'posts_per_page' => 10,
    'paged' => $paged
);

$custom_query = new WP_Query($args);

if ($custom_query->have_posts()) :
    while ($custom_query->have_posts()) : $custom_query->the_post();
?>
    <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <div class="entry-content">
            <?php the_excerpt(); ?>
        </div>
    </article>
<?php
    endwhile;
    
    // Pagination
    echo '<div class="pagination">';
    echo paginate_links(array(
        'total' => $custom_query->max_num_pages,
        'current' => $paged,
        'prev_text' => '&laquo; Previous',
        'next_text' => 'Next &raquo;'
    ));
    echo '</div>';
    
    wp_reset_postdata();
else :
    echo 'No posts found';
endif;
?>

Alternative Plugin Solutions:

  1. Pods - Powerful custom post type and field management
  2. Toolset Types - Advanced custom post type handling and display
  3. Custom Post Type UI - User-friendly interface for creating and managing custom post types

Remember to replace 'your_custom_post_type' with your actual custom post type name, and adjust the query parameters according to your needs.