Gathering Custom post types via tags Gathering Custom post types via tags wordpress wordpress

Gathering Custom post types via tags


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();


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.


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.