Home › Forums › Front-end Issues › Display only "Featured" custom posttypes on taxonomy page
Hi there,
On a custom taxonomy page I want to display only the featured posts. Post can be made Featured with the true / false field of ACF.
The featured post are displayed, but not based on the term of the taxonomy page.
This is my code (placed on a taxonomy page):
<?php
// The Query
$the_query = new WP_Query(
array(
'post_type' => 'cursus',
'posts_per_page' => 3,
'meta_key' => 'uitgelicht_bij_land',
'meta_value' => '1' )
);
// The Loop
if ( $the_query->have_posts() ) {?>
<h3>Uitgelichte taalcursussen in <?php single_term_title(); ?></h3>
<?php while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'cursusloop', get_post_format() );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata(); ?>
How can I get this to work on only de term of the taxonomy page?
Regards,
Dennis.
if all you want to show on this page is the featured posts and the rest of the posts in the term will never be shown then instead of doing a custom query on the page you need to use a pre_get_posts filter and alter the query that WP is already doing.
add_action('pre_get_posts', 'your_function_name_here');
function your_function_name_here($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_tax('your-taxonomy-here')) {
$query->set('meta_key', 'uitgelicht_bij_land');
$query->set('meta_value', '1');
$query->set('posts_per_page', 3);
}
}
If on the other hand you are showing other posts as well then you do need to do a custom query, but you need to add a tax_query. You can get the current taxonomy like
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
then you can use these values in the tax query
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => array($term_id)
)
)
Thank you so much,
That worked at first try!
I do have a feeling I could have made the code more efficient. Setting variables twice?
Or is it correct right now. (it works 😉 )
<?php
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
add_action('pre_get_posts', 'uitgelichte_cursusloop_land');
function uitgelichte_cursusloop_land($query) {
if (is_admin() || !$query->is_main_query()) {
return;
}
if ($query->is_tax($taxonomy )) {
$query->set('meta_key', 'uitgelicht_bij_land');
$query->set('meta_value', '1');
$query->set('posts_per_page', 30);
}
}
// The Query
$the_query = new WP_Query(
array(
'post_type' => 'cursus',
'posts_per_page' => 30,
'meta_key' => 'uitgelicht_bij_land',
'meta_value' => '1',
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'terms' => array($term_id)
)
)
)
);
// The Loop
if ( $the_query->have_posts() ) {?>
<h3>Uitgelichte taalcursussen in <?php single_term_title(); ?></h3>
<?php while ( $the_query->have_posts() ) {
$the_query->the_post();
get_template_part( 'cursusloop', get_post_format() );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata(); ?>
The topic ‘Display only "Featured" custom posttypes on taxonomy page’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.