Get custom post types in WordPress

Get custom post types in WordPress

To get custom post types in wordpress, pass post type data i.e. taxonomy, term id, post type and post_per_page in `WP_Query`, `query_posts` or `get_post` function. I will use `WP_Query` 

Difference between query_posts(), get_posts() and WP_Query

query_posts(): Using query_posts is simple way to get posts, but sometimes it fails some conditions i.e. pagination. It makes the website slow because it executes the sql queries in many times. Not good to use query_posts()

get_posts(): get_posts will used with foreach. it will return an array and will used in anywhere.

Also Read: Create multiple headers, footers, sidebars for different templates in WordPress

WP_Query:  WP_Query is safer, efficient to used and can also work with pagination.

Note: posts_per_page `-1` means all posts

$args = array(
            'posts_per_page' => -1,
            'post_type' => 'POST_TYPE_HERE',
            'tax_query' => array(
                array(
                    'taxonomy' => 'TAXONOMY_HERE',
                    'field' => 'term_id',
                    'terms' => TEMR_ID_HERE,
                )
            )
        );              

        $loop = new WP_Query($args);
        if($loop->have_posts()) {
            while($loop->have_posts()) : $loop->the_post();
            $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_id()), 'thumbnail_size' );
            $imageURL = $src[0];
            ?>
            <a href="<?php the_permlink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
                <h3><?php the_title(); ?></h3>
            </a>
            endwhile;
        }

 

Also Read: Get logged in user info in WordPress

To get custom post types in wordpress, pass post type data i.e. taxonomy, term id, post type and post_per_page in `WP_Query`, `query_posts` or `get_post` function. I will use `WP_Query` 

Difference between query_posts(), get_posts() and WP_Query

query_posts(): Using query_posts is simple way to get posts, but sometimes it fails some conditions i.e. pagination. It makes the website slow because it executes the sql queries in many times. Not good to use query_posts()

get_posts(): get_posts will used with foreach. it will return an array and will used in anywhere.

Also Read: Create multiple headers, footers, sidebars for different templates in WordPress

WP_Query:  WP_Query is safer, efficient to used and can also work with pagination.

Note: posts_per_page `-1` means all posts

$args = array(
            'posts_per_page' => -1,
            'post_type' => 'POST_TYPE_HERE',
            'tax_query' => array(
                array(
                    'taxonomy' => 'TAXONOMY_HERE',
                    'field' => 'term_id',
                    'terms' => TEMR_ID_HERE,
                )
            )
        );              

        $loop = new WP_Query($args);
        if($loop->have_posts()) {
            while($loop->have_posts()) : $loop->the_post();
            $src = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_id()), 'thumbnail_size' );
            $imageURL = $src[0];
            ?>
            <a href="<?php the_permlink(); ?>" title="<?php the_title(); ?>">
<img src="<?php echo $url; ?>" alt="<?php the_title(); ?>">
                <h3><?php the_title(); ?></h3>
            </a>
            endwhile;
        }

 

Also Read: Get logged in user info in WordPress

Please let me know what your thoughts or comments are on this article. If you have any suggestion or found any mistake in this article then please let us know.

Latest Comments

Add your comment

Close