So I have a custom Taxonomy 'Sectors'.
I am trying to display all the single posts in 'Sectors' in a list, just a basic list.
I currently do have it so, it will display the category, then a sub category, then the list from there.
I also have this, which lists all posts - (NOT TAXONOMY) :
<?php
$uncat = get_cat_ID('uncategorised');
$args = array(
'posts_per_page' => 5,
'category__not_in' => array($uncat)
);
$loop = new WP_Query( $args );
while ($loop->have_posts() ) : $loop->the_post();
?>
<div class="col-md-12 col-sm-12 col-xs-12" style="padding-left:0; padding-right:0;">
<a href="<?php echo get_permalink(); ?>">
<div class="postsize">
<div class="leftfloat" style="float: left; padding-right:20px;">
<?php echo get_the_post_thumbnail( $page->ID, 'categoryimage', array('class' => 'faqposts')); ?>
</div>
<div class="contentfaq">
<h3><?php the_title(); ?></h3>
<span class="entry-date-orange"><strong><?php echo get_the_date(); ?></strong></span>
<?php
foreach((get_the_category()) as $category) {
echo ' | ' . $category->cat_name;
}
?>
<p style="margin-top:10px";><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
is there a way, to use this code below, to ONLY get items from the taxonomy 'sectors'?
For this to work, you will need to get all the terms belonging to your taxonomy, and then use a tax_query
to query the correct posts. For effeciency, we will only get the term ids and not the complete objects
(NOTE: The code is untested and requires PHP 5.4+. Also, just confirm the taxonomy name)
$term_ids = get_terms( 'SectorCategories', ['fields' => 'ids'] );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = [
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => [
[
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
]
],
// REST OF YOUR ARGUMENTS
];
$loop = new WP_Query( $args );
For older PHP versions, use the following code
$term_ids = get_terms( 'SectorCategories', array( 'fields' => 'ids' ) );
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
$args = array(
'post_type' => 'sectors',
'posts_per_page' => 5,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'SectorCategories',
'terms' => $term_ids,
)
),
// REST OF YOUR ARGUMENTS
);
$loop = new WP_Query( $args );
A very basic loop for the above query would look something like this
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) {
$loop->the_post();
the_title();
}
next_posts_link( 'Next posts', $loop->max_num_pages );
previous_posts_link( 'Previous posts' );
wp_reset_postdata();
}