Display a single category in blog section

The question:

I have been struggling with a problem, I have a webpage running on WordPress, most of the things work as expected, except one: in the blog section page (the one that is configured in settings/reading -> Posts page), all posts are shown. I have a specific category that should be presented there, but I don’t know how to alter the loop to work as expected. Here´s the code of the index page:

<?php
$class_archive  = '';
$is_grid_layout = get_theme_mod( 'thim_front_page_cate_display_layout', false );
$layout_type    = $is_grid_layout ? 'grid' : '';
if ( $layout_type == 'grid' ) {
    $class_archive = ' blog-switch-layout blog-list';
    global $post, $wp_query;
    
    if ( is_category() ) {
        $total = get_queried_object();
        $total = $total->count;
    } elseif ( ! empty( $_REQUEST['s'] ) ) {
        $total = $wp_query->found_posts;
    } else {
        $total = wp_count_posts( 'post' );
        $total = $total->publish;
    }

    if ( $total == 0 ) {
        echo '<p class="message message-error">' . esc_html__( 'There are no available posts!', 'eduma' ) . '</p>';

        return;
    } elseif ( $total == 1 ) {
        $index = esc_html__( 'Showing only one result', 'eduma' );
    } else {
        $courses_per_page = absint( get_option( 'posts_per_page' ) );
        $paged            = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;

        $from = 1 + ( $paged - 1 ) * $courses_per_page;
        $to   = ( $paged * $courses_per_page > $total ) ? $total : $paged * $courses_per_page;

        if ( $from == $to ) {
            $index = sprintf(
                esc_html__( 'Showing last post of %s results', 'eduma' ),
                $total
            );
        } else {
            $index = sprintf(
                esc_html__( 'Showing %s-%s of %s results', 'eduma' ),
                $from,
                $to,
                $total
            );
        }
    }
}

I’ve added:

$args = array(
    cat => 200
);
  
// Custom query.
$wp_query = new WP_Query( $args );

Without any success.

Thanks in advance for your help.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

In WordPress, we have what is called as “main query” which points to the global $wp_query object (which is an instance of the WP_Query class), and this main query is run on page load after WordPress parses query arguments from the current URL (or the relevant query for the matching rewrite rule for that URL).

And you should never change/overwrite that variable, or even make the custom query as in your code, because doing so can break a lot of things and also affects the page performance. See query_posts() for further details (but no, do not use that function, either).

The proper way to alter the main WordPress query

.. is by using hooks like pre_get_posts. And on that hook, you can use conditional tags like is_home(), is_front_page() and is_category() to target a specific page.

So here’s an example for your case (and this code would go in the theme functions file):

add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $query ) {
    // If it's the blog page and $query points to the main WordPress query, then
    // we set "cat" to a specific category ID.
    if ( is_home() && $query->is_main_query() ) {
        $query->set( 'cat', 200 );
    }
}

Further Reading


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment