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

wp query - wp_query is showing posts from other categories

programmeradmin2浏览0评论

I am trying to make a custom loop that will display the latest 5 posts from select categories, all using a single query. I want the loop to display the category name followed by the 5 posts, and then the next category, etc.

I followed this answer and it seems to do what I want, except it doesn't output the categories properly.

For example, let's say I want to display categories 100,101,102 and 103, it should display like this:

100 Post 1 Post 2 Post 3 Post 4 Post 5

101 Post 1 Post 2 Post 3 Post 4 Post 5

etc.


The Problem

However, currently, if there is a post in category 100, and also in category 250, it will also display the title of category 250 too, like so

250 Post 1

How do I get it that it ONLY displays the categories specified?


Here's the code I have so far, based on this.

<?php

$args = array(
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            // arguments for your query
            'taxonomy' => 'category',
            'field' =>  'id',
            'terms' => $selected_categories, // $selected_categories contains an array of post IDs.
        )
    )
);

$query = new WP_Query($args);   
$q = array();

while ( $query->have_posts() ) { 

    $query->the_post(); 

    $a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';

    $categories = get_the_category();

    foreach ( $categories as $key=>$category ) {

        $b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';    

    }

    $q[$b][] = $a; // Create an array with the category names and post titles
}

/* Restore original Post Data */
wp_reset_postdata();

foreach ($q as $key=>$values) {
    echo $key;

    echo '<ul>';
        foreach ($values as $value){
            echo '<li>' . $value . '</li>';
        }
    echo '</ul>';
}

?>

Thanks for any help!

I am trying to make a custom loop that will display the latest 5 posts from select categories, all using a single query. I want the loop to display the category name followed by the 5 posts, and then the next category, etc.

I followed this answer and it seems to do what I want, except it doesn't output the categories properly.

For example, let's say I want to display categories 100,101,102 and 103, it should display like this:

100 Post 1 Post 2 Post 3 Post 4 Post 5

101 Post 1 Post 2 Post 3 Post 4 Post 5

etc.


The Problem

However, currently, if there is a post in category 100, and also in category 250, it will also display the title of category 250 too, like so

250 Post 1

How do I get it that it ONLY displays the categories specified?


Here's the code I have so far, based on this.

<?php

$args = array(
    'posts_per_page' => -1,
    'tax_query' => array(
        array(
            // arguments for your query
            'taxonomy' => 'category',
            'field' =>  'id',
            'terms' => $selected_categories, // $selected_categories contains an array of post IDs.
        )
    )
);

$query = new WP_Query($args);   
$q = array();

while ( $query->have_posts() ) { 

    $query->the_post(); 

    $a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';

    $categories = get_the_category();

    foreach ( $categories as $key=>$category ) {

        $b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';    

    }

    $q[$b][] = $a; // Create an array with the category names and post titles
}

/* Restore original Post Data */
wp_reset_postdata();

foreach ($q as $key=>$values) {
    echo $key;

    echo '<ul>';
        foreach ($values as $value){
            echo '<li>' . $value . '</li>';
        }
    echo '</ul>';
}

?>

Thanks for any help!

Share Improve this question edited May 4, 2020 at 17:22 Edegist asked May 4, 2020 at 3:52 EdegistEdegist 1331 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I have used Custom Select Query to gather all information in a single call and hope it may work out.

global $wpdb;
$selected_cat_id = '6,7,8,9';
$post_count = 5;

$custom_query = "SELECT $wpdb->posts.ID, $wpdb->posts.post_title, $wpdb->terms.name FROM $wpdb->posts  
LEFT JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
LEFT JOIN $wpdb->terms ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->terms.term_id) 
WHERE 1=1 AND ($wpdb->term_relationships.term_taxonomy_id IN ($selected_cat_id))
AND $wpdb->posts.post_type = 'post' AND ($wpdb->posts.post_status = 'publish' OR $wpdb->posts.post_status = 'private') ORDER BY $wpdb->posts.post_date ASC";
$pageposts = $wpdb->get_results($custom_query, OBJECT);

$cat_posts = array();
foreach($pageposts as $post){
    $cat_posts[$post->name][$post->ID] = $post->post_title;
}

foreach($cat_posts as $key => $values) {
    $category_id = get_cat_ID( $key );
    echo '<a href="'.get_category_link($category_id).'">'.$key.'</a>';
    $values = array_slice($values,0,$post_count,true);
    echo '<ul>';
        foreach ($values as $post_id => $value){
            echo '<li><a href="'.get_permalink($post_id).'">' . $value . '</a></li>';
        }
    echo '</ul>';
}

Use 'category_name' as direct argument in your $args array, not inside your tax_query array. I always displayed posts of a category on my pages in that way.

发布评论

评论列表(0)

  1. 暂无评论