I currently have a custom post type, named Sectors. This has categories too.
add_action( 'init', 'wpsites_custom_post_type' );
function wpsites_custom_post_type() {
register_post_type( 'sectors',
array(
'labels' => array(
'name' => __( 'Sectors' ),
'singular_name' => __( 'sector' ),
),
'has_archive' => true,
'hierarchical' => true,
'menu_icon' => 'dashicons-heart',
'public' => true,
'rewrite' => array( 'slug' => 'sectors', 'with_front' => false ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'sectors', 'post_tag' ),
));
}
In here, I have individual sectors, which can have tags attatched
What I am currently trying to do, is add tags to a certain sector, and the tagged sector will be a 'featured sector' In its category page.
Using the code below, on my taxonomy-sectors.php page, I can do this:
<?php
$args = array(
'tag_slug__and' => array('sector1'),
'post_type' => array( 'sectors' )
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<a href="<?php echo get_permalink(); ?>">
<?php echo "<div class='col-md-6' style='margin-bottom:20px;'>"; ?>
<div class="row mobilemargin">
<div class="categorytiletextsector1">
<div class="col-md-6 col-sm-6 col-xs-12 nopr"><?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'sector1img hovereffect')); ?> </div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="testdiv">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div>
</div>
<?php echo "</div>"; ?>
</a>
The Issue is, this displays these tagged sectors on EVERY category page, as all categories use the taxonomy page. Is there a way, that it would only display these tagged sector pages to its own category? No sectors can be in 1 category, if that helps.
Any help would be great, this has been bugging me for days now :(
EDIT
The code below is from an answer, but this does not return anything now, is there something missing??
$args = array(
'tag_slug__and' => array( 'sector1' ),
'post_type' => array( 'sectors' ),
'tax_query' => array(
array(
'taxonomy' => 'sectors',
'terms' => get_queried_object_id(),
),
),
);