Support » Fixing WordPress » WP_Query not working

  • Resolved MathCiet

    (@mathciet)


    Could anyone explain why this query isn’t working?
    It still shows the post with category name ‘homepage’…

    <?php
    	$query = new WP_Query( 'category_name=-homepage');
    ?>
    <?php if ( $query->have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php
    			get_template_part( 'content', 'news' );
    		?>
    	<?php endwhile; ?>
    	<?php the_posts_navigation(); ?>
    	<?php else : ?>
    		<?php get_template_part( 'content', 'none' ); ?>
    	<?php endif; ?>
Viewing 14 replies - 1 through 14 (of 14 total)
  • Viamultimedia

    (@viamultimedia)

    Hello,

    I don’t understand what you want, but i Think is homepage and not -homepage

    No ?

    Thread Starter MathCiet

    (@mathciet)

    I want to exclude the posts tagged with homepage.
    I also tried $query = new WP_Query( 'cat=-11' ); but it isn’t working either.

    Viamultimedia

    (@viamultimedia)

    Are you try category__not_in in your query ?

    Thread Starter MathCiet

    (@mathciet)

    Also not working:
    $query = new WP_Query( 'category__not_in => 11' );

    Viamultimedia

    (@viamultimedia)

    Ok

    Ihave found this

    <?php
    $temp_query = $wp_query;
    $exclude = get_cat_ID('showreel');
    query_posts("cat=-$exclude&post_type=projects&showposts=4&order=ASC");?>

    Try width this exemple. Do an variable $exclude and add -$exclude to your query…

    I hope it will work !

    Thread Starter MathCiet

    (@mathciet)

    <?php
    	$args = array(
    	'category_name' => 'homepage',
    	);
    	$query = new WP_Query( 'cat=-$args' );
    ?>

    isn’t working either 🙁

    Viamultimedia

    (@viamultimedia)

    I don’t know if is a googd solution but you can do a pre get post if you dont want to show posts by the category…

    In your functions.php

    function demo_exclude_category( $wp_query ) {
    
        // Add the category to an array of excluded categories. In this case, though,
        // it's really just one.
        $excluded = array( '-1' );
    
        // Note that this is a cleaner way to write: $wp_query->set('category__not_in', $excluded);
        set_query_var( 'category__not_in', $excluded );
    
    }
    add_action( 'pre_get_posts', 'demo_exclude_category' );

    Change the good ID of your category…

    Thread Starter MathCiet

    (@mathciet)

    Maybe, but I only want it on one page… 🙂

    Viamultimedia

    (@viamultimedia)

    Hello

    I have try it and it’s work !

    <?php
        query_posts(array(
            'post_type' => 'post',
            'showposts' => 5,
            'cat' => '-1'
        ) );
    ?>
    <?php if ( $query->have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php
    			get_template_part( 'content', 'news' );
    		?>
    	<?php endwhile; ?>
    	<?php the_posts_navigation(); ?>
    	<?php else : ?>
    		<?php get_template_part( 'content', 'none' ); ?>
    	<?php endif; ?>
    Thread Starter MathCiet

    (@mathciet)

    Still not working: suddenly I get pages in the newsfeed :p

    Thread Starter MathCiet

    (@mathciet)

    You also need to change while ( have_posts() ) : the_post(); to while ( $query->have_posts() ) : $query->the_post();

    Viamultimedia

    (@viamultimedia)

    it’s work now ?

    Thread Starter MathCiet

    (@mathciet)

    Yes! 🙂
    Thank you for your help!

    Viamultimedia

    (@viamultimedia)

    You’re welcome ! I haven’t seen the $query->have_posts(). I’m tired !
    Happy it’s work have a good day !

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘WP_Query not working’ is closed to new replies.