最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Exclude categories with only sticky posts OR count sticky post of specific category

programmeradmin2浏览0评论

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 2

category 2
post 3
post 4

category 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 2

category 2
post 3
post 4

category 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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

I 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;

}

发布评论

评论列表(0)

  1. 暂无评论