I have set up my custom post type called ‘sectors’, using the code below:
register_post_type( 'sectors', array( 'labels' => array( 'name' => __( 'Sectors' ), 'singular_name' => __( 'sectors' ), ), 'has_archive' => true, 'hierarchical' => true, 'menu_icon' => 'dashicons-heart', 'public' => true, 'rewrite' => array( 'slug' => 'your-cpt', 'with_front' => false ), 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ), 'taxonomies' => array( 'your-cpt-type', 'post_tag' ), )); }
This has allowed me to add ‘tags’ to the custom post type pages.
Now, I am trying to display pages fron this custom post types by certain tags.
I have managed to do this with posts, by using the following code:
<?php $args = array('tag_slug__and' => array('featuredpost1')); $loop = new WP_Query( $args ); while ($loop->have_posts() ) : $loop->the_post(); ?> <h5 class="captext"><?php the_title(); ?></h5> <hr> <div style="float: left; padding-right:20px;"> <?php the_post_thumbnail( 'thumb' ); ?> </div> <?php the_excerpt(); ?> <a href="<?php echo get_permalink(); ?>"> Read More...</a> <?php endwhile; ?> <?php wp_reset_query(); ?>
This will get all posts which have the tag ‘featuredpost1’.
How is this possible with custom post types?
EDIT/UPDATE:
This does work now, is there a way I can use this functionality on a different page? For example, on my homepage get the posts via tags, so whatever is updated on this page will update on the homepage??
Advertisement
Answer
If you add ::
$args = array( 'post_type' => array( 'sectors' ) //, 'multiple_types_after_commas' ) ); $query = new WP_Query( $args );
or
$query = new WP_Query( 'post_type=sectors' );
This will help you target your post type with your query.
It will look like
$args = array( 'tag_slug__and' => array('featuredpost1'), 'post_type' => array( 'sectors' ) ); $loop = new WP_Query( $args ); while ($loop->have_posts() ) : $loop->the_post();