HTML Code
Development

How to Get WordPress Custom Posts using PHP

Custom post types power wordpresses powerful templating system

8 months ago | The Author, Charlie Cooper by Charlie Cooper

One of the most powerful features of WordPress is the ability to add custom post types to your website.

In this article, we’re going to look at using WP Query to show posts from a custom post type anywhere on your website.

Let me explain – Let’s say that you have a video website. Now, most people starting out would make the first video page, then copy the code or duplicate the page for every video and change what was needed.

WordPress custom post types make this much faster because it allows you to make a single template to be used by all video pages. This means making a new page is as simple as typing in the title and pasting in a link to the video.

If you’re familiar with WordPress, you’ll have seen this in action every time you post a blog.

Let’s say that you want to get 3 random videos for your homepage to give people different inspirations every time they visit.

Let’s jump into the code. The way WP query works is that you tell it what you want, then ask it to do the query. Here’s how we tell the query what we’re looking for:

$args = array(
    'post_type' => 'videos',
    'orderby' => 'rand',
    'posts_per_page' => 3
);

You can see we’ve specified we’d like 3 posts, in random order from the videos post type. Now we’ll use a wp_query to actually ask WordPress to get them for us, and we’ll pass in the $args requirements we made before.

$random_videos_query = new WP_Query( $args );
Now we need to output them. I’ll keep this simple so it’s easy to follow along.
if ( $random_videos_query->have_posts() ) {
    while ( $random_videos_query->have_posts() ) {
        $random_videos_query->the_post();
        echo get_the_title();
    }
}
wp_reset_postdata();

We’re essentially asking WordPress if it found any posts, then saying for every post you found, do XYZ. In this case, we’re just asking it to output the title.

If we ran this code on the homepage of a website that has a video post type with posts, it would output 3 random video titles.

There’s so much you can do with this, and so far we’ve only scratched the surface to keep things simple. The next level is to link the titles to their pages, like this:

if ( $random_videos_query->have_posts() ) {
    while ( $random_videos_query->have_posts() ) {
        $random_videos_query->the_post();
        echo '<a href="'. get_the_permalink() .'">' . get_the_title() . '</a>';
    }
}
wp_reset_postdata();
it’s important to remember to wrap this code in PHP tags otherwise it will output as text. Here’s the full code with tags:
<?php
$args = array(
    'post_type' => 'videos',
    'orderby' => 'rand',
    'posts_per_page' => 3
);
$random_videos_query = new WP_Query( $args );
if ( $random_videos_query->have_posts() ) {
    while ( $random_videos_query->have_posts() ) {
        $random_videos_query->the_post();
        // Do something with each post, like display the title or excerpt
    }
}
wp_reset_postdata();
?>
I hope this helps if you’re starting out with using WordPress custom post types and PHP. Feel free to adjust the code to your heart’s content!
Get in touch today
Great Pricing
Unique Websites
Get found on Google