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.
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;
}
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.
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;
}
Recommanded Articles
- Generate Dummy Posts in WordPress
- How to add captcha in Contact Form 7
- how to create a cron job in wordpress
- How to get form data in Contact Form 7
- Insert Update Delete Select query in WordPress
- How to increase website speed
- Create plugin in WordPress in which create table in DB on activate
- Create new tab under my account WooCommerce frontend
- Custom image sizes in WordPress
- Get posts by meta key and meta value in WordPress
Latest Comments