I want to create a page which displays all categories with posts. My goal is to exclude categories which only has sticky post(s). That's the part which isn't working so far.
My current results looks like this:
category 1
post 1
post 2category 2
post 3
post 4category 3
category 4
post 6
post 7
...so in this example the wp_query ignores the sticky post of "category 3", but still the category is shown because it has one hidden sticky post. "category 3" should disappear in this example.
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 1,
) );
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
// COUNT TO TEST IT:
echo "Posts: " . $category->category_count . " | " ;
echo "Sticky Posts:" . count(get_option('sticky_posts')) . "<br>";
$cat_args = array (
'category_name' => $category->name,
'posts_per_page' => 10,
'ignore_sticky_posts' => 0,
'post__not_in' => get_option('sticky_posts')
);
$cat_query = new WP_Query( $cat_args );
if ( $cat_query->have_posts() ) : while ( $cat_query->have_posts() ) : $cat_query->the_post();
the_title(); ?><br><?php
endwhile; endif;
wp_reset_query();
How do I solve this? Maybe be counting sticky_posts of one specific category? I don't know how to address that...
Or is there another solution?
I want to create a page which displays all categories with posts. My goal is to exclude categories which only has sticky post(s). That's the part which isn't working so far.
My current results looks like this:
category 1
post 1
post 2category 2
post 3
post 4category 3
category 4
post 6
post 7
...so in this example the wp_query ignores the sticky post of "category 3", but still the category is shown because it has one hidden sticky post. "category 3" should disappear in this example.
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 1,
) );
foreach ( $categories as $category ) {
printf( '<a href="%1$s">%2$s</a><br />',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
// COUNT TO TEST IT:
echo "Posts: " . $category->category_count . " | " ;
echo "Sticky Posts:" . count(get_option('sticky_posts')) . "<br>";
$cat_args = array (
'category_name' => $category->name,
'posts_per_page' => 10,
'ignore_sticky_posts' => 0,
'post__not_in' => get_option('sticky_posts')
);
$cat_query = new WP_Query( $cat_args );
if ( $cat_query->have_posts() ) : while ( $cat_query->have_posts() ) : $cat_query->the_post();
the_title(); ?><br><?php
endwhile; endif;
wp_reset_query();
How do I solve this? Maybe be counting sticky_posts of one specific category? I don't know how to address that...
Or is there another solution?
Share Improve this question asked Jul 4, 2019 at 16:49 allensteinallenstein 11 bronze badge1 Answer
Reset to default 0I found a solution by rewiting the foreach and the WP_query structure:
$categories = get_categories( array(
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 1,
) );
foreach ( $categories as $category ) {
$cat_args = array (
'category_name' => $category->name,
'posts_per_page' => 10,
'ignore_sticky_posts' => 0,
'post__not_in' => get_option('sticky_posts'),
'orderby' => 'name',
'order' => 'ASC',
'parent' => 0,
'hide_empty' => 1,
);
$cat_query = new WP_Query( $cat_args );
// The Loop
if ( $cat_query->have_posts() ) :
printf( '<br/><a href="%1$s">%2$s</a><br/>',
esc_url( get_category_link( $category->term_id ) ),
esc_html( $category->name )
);
while ( $cat_query->have_posts() ) : $cat_query->the_post();
the_title();
?><br/><?php
endwhile; endif;
}