17

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??

3
  • Anyone have any ideas?
    – user2209033
    Apr 28, 2015 at 8:22
  • have you added "post-type" => "sectors" to your args?
    – Cayce K
    Apr 29, 2015 at 16:51
  • 1
    On which page/template are you doing this. Do you really need a custom query Apr 29, 2015 at 19:34

3 Answers 3

12
+50

Wordpress Query Parameters

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();
1
  • 1
    Thank you, this worked perfectly! Also, thank you to Zork for your answer, and Selva.
    – user2209033
    Apr 30, 2015 at 10:47
3

Cayce K's solution will work perfectly. I have a second way to offer:

First: Add your Custom Post Type to the main query. You can achive this by adding a few lines to your functions.php.

<?php   
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
    function add_my_post_types_to_query( $query ) {
        // Leave the query as it is in admin area
        if( is_admin() ) {
            return $query;
        }
        // add 'sectors' to main_query when it's a tag- or post-archive
      if ( is_tag() && $query->is_main_query() || is_archive() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'sectors', 'add_more_here' ) );
      return $query;
    }
?>

Second: After doing so you can use the archive.php, the tag.php or a tag-myTagName.php in your theme to show an archive-page for that tag including your Custom Post Type 'sectors'. You won't have to set up a special query, just add a link to the desired tag to one of your menus - your standard loop will do the rest.

Hint:

When you just want to create an archive-page for your complete Custom Post Type 'sectors' you can also use the WP plugin Post Type Archive Link.

1
  • nice out of the box thinking. that is some fun knowledge to have. thank you for posting this answer!
    – Cayce K
    Apr 29, 2015 at 20:34
1

If your looking for a custom post type with tag name means, you will need to specify that in the query arguments:

  <?php $query = new WP_Query( array( "post_type" => "sectors", "tag" => "featuredpost1" ) );
        
   while ($query->have_posts()) : $query->the_post();
        
   the_title();
        
  endwhile; ?>

May this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.