0

I've a weird problem, some posts appears in categories where they are not in. When I look in my backoffice and filter by categories, some post appears there but they are not checked in.

The resultat is that in the front office they appear too.

This is my category.php (but I don't think it's the matter)

<?php
get_header();
?>

<section id="wrapper" class="page <?php echo get_query_var('cat'); ?>">
    <div id="container">

        <?php 

            $category = get_category(get_query_var('cat'));
            $cat_id = $category->cat_ID;
            query_posts('showposts=1&cat='.$cat_id);

            if ( have_posts() ) : 

                while ( have_posts() ) : the_post();

                get_template_part( 'content', get_post_format() );

                endwhile;

            endif;
        ?>
    </div>
</section>
<?php
    get_footer();
?>

I looked in the table "_term_relationships" and everything is right, they're not in the wrong categories.

So maybe someone have a clue to find out ?

PS : I'm using WPML, but if I desactive it, it's the same problem

H4mm3R
  • 245
  • 4
  • 15
  • 1
    Drop your custom query, use the main loop. Also never ever use `query_posts`, it is just a really bad function to use to create custom queries. – Pieter Goosen Oct 09 '14 at 13:14
  • You mean my custom jquery in the backoffice ? Because I dont use one – H4mm3R Oct 09 '14 at 13:18

2 Answers2

0

You should not use query_posts(), see (https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts)

try this:

<?php 

            $category = get_category(get_query_var('cat'));
            $cat_id = $category->cat_ID;

            $args = array( 'category' => $cat_id );


             $query2 = new WP_Query($args);

            if ( $query2->have_posts() ) : 

                while ( $query2->have_posts() ) : 
                    $query2->the_post();

                    get_template_part( 'content', get_post_format() );

                endwhile;

            endif;
        ?>
Community
  • 1
  • 1
tinos
  • 562
  • 6
  • 12
  • Well, in array it's not 'category' but 'cat'. Thx for the help, but the problem stay the same. – H4mm3R Oct 09 '14 at 14:39
  • if you remove the query_posts and leave only the loop? whats the url? – tinos Oct 09 '14 at 14:56
  • http://www.ultraflux.net/category/expertise/ http://www.ultraflux.net/category/services/ Seen, they have the same article, but this article is just in expertise and should not appear in services – H4mm3R Oct 10 '14 at 13:00
0

First of all, never use query_posts to construct any type of query

Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

Secondly, never change the the main query for a custom query on any type of archive page or home page. The correct way is to use pre_get_posts to alter the query variables before the main query executes. Check out this post I've done a while ago

Thirdly, category pages in Wordpress does work in a strange way. When a category page is visited, it will display posts from the selected category and posts from the selected category's child categories. I bet this is what you are seeing. This is pretty normal behavior. If you need to change this, have a look at this answer on WPSE by @ialocin. For the benefit of this answer, here is the solution

add_filter( 
    'parse_tax_query', 
    'wpse163572_do_not_include_children_in_category_archive_parse_tax_query' 
);
function wpse163572_do_not_include_children_in_category_archive_parse_tax_query( $query ) {
    if ( 
        ! is_admin() 
        && $query->is_main_query()
        && $query->is_category()
    ) {
        // as seen here: https://wordpress.stackexchange.com/a/140952/22534
        $query->tax_query->queries[0]['include_children'] = 0;
    }
}
Community
  • 1
  • 1
Pieter Goosen
  • 9,768
  • 5
  • 35
  • 55
  • Thanks. I learned a lot with your post. But no, my categories are not children. They are 2 parents... – H4mm3R Oct 10 '14 at 10:28
  • Do you have any custom queries that you haven't reset, or any instance of `pre_get_posts` – Pieter Goosen Oct 10 '14 at 10:31
  • Have you gone back to the main loop on your page. Dit you delete `query_posts`. Copy your index.php and rename it category.php – Pieter Goosen Oct 10 '14 at 13:11
  • Not yet, I'll do it asap. But to be honest, I don't think this will resolve the dirty problem. Because in my backoffice (where I didn't change anything), there is the same problem... – H4mm3R Oct 10 '14 at 14:04
  • Custom queries on archive pages are **always** problematic. If you have the same problem back end, you are most probably using `pre_get_posts` somewhere in your theme **wrongly**. I would start there – Pieter Goosen Oct 10 '14 at 14:08