WordPress.org

Ready to get started?Download WordPress

Forums

Exclude posts by category on index page (12 posts)

  1. AndrewHodges
    Member
    Posted 1 year ago #

    I am sorry to post this but I have gone blind looking at it. I have entered this in my main index page

    <?php
    query_posts($query_string.'&cat=-51');
    if (have_posts()) : while (have_posts()) : the_post();

    // The following determines what the post format is and shows the correct file accordingly

    Simple or so you would think. It does not work and I don't know why. I have tried

    query_posts('cat=-51');

    But that does not work either. The same code in a template file works fine. Can anyone stop me going mad!

  2. redrocksrover2
    Member
    Posted 1 year ago #

    Depending on what you're trying to accomplish, query_posts() may not be the best- or preferred- way to get it done. But assuming it is, see below. Also, be sure that the category ID you're trying to exclude is actually "51" - if it isn't, this won't do you any good...

    $args = array(
           'cat' => '-51',
           'posts_per_page' => 10,
           'order' => 'DESC'
    );
    query_posts($args);

    ...Then your loop. After your loop, you'll probably want to reset the query:

    endwhile; // End of the loop
    
    // Reset
    wp_reset_query();

    If you want to display every post in your blog, you can set 'posts_per_page' to '-1'. Then depending on your theme, you can also paginate the output with 'paged' => $paged.

    See: http://codex.wordpress.org/Function_Reference/query_posts

  3. esmi
    Forum Moderator
    Posted 1 year ago #

    Can I ask if you're modifying the main Loop in index.php or are trying to create a secondary query & Loop?

  4. AndrewHodges
    Member
    Posted 1 year ago #

    The main loop in the query page

  5. AndrewHodges
    Member
    Posted 1 year ago #

    Redrocksrover:

    Thanks for that. Not sure I see the material difference but I have tried it and it makes no difference. My page http://fernleafconsulting.co.uk/FLWP/blog/ should not show the QR post as it is cat 51 but it does. My page http://fernleafconsulting.co.uk/FLWP/contact-shortcuts/ (template page but using same code loop) uses the same statement 'cat=51' and works fine.

  6. alchymyth
    Forum Moderator
    Posted 1 year ago #

    an alternative way is to use 'pre_get_posts'; whose Codex chapter has a matching example:

    http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts#Example:_Exclude_categories_on_your_main_page

    this code goes into functions.php of the theme;
    adapted:

    function exclude_category( $query ) {
        if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', '-51' );
        }
    }
    add_action( 'pre_get_posts', 'exclude_category' );
  7. AndrewHodges
    Member
    Posted 1 year ago #

    Thanks for the function and its a little more elegant for future use but does not solve the issue. Cat 51 is still appearing? I have checked again and again to make sure it is the right cat etc but as I say it works else where.

  8. alchymyth
    Forum Moderator
    Posted 1 year ago #

    have you verified that the 'blog' page is done by the main index.php template?

    a similar approach to your first idea:

    before the start of the loop, add:

    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'cat' => '-51' ) );
    query_posts( $args );

    or:

    global $wp_query;
    $args = array_merge( $wp_query->query_vars, array( 'category__not_in' => array( 51 ) ) );
    query_posts( $args );

    http://codex.wordpress.org/Function_Reference/query_posts#Preserving_Existing_Query_Parameters

  9. AndrewHodges
    Member
    Posted 1 year ago #

    Yes I put a little tracking text in 'Hi your on the right page' just before I started to pull my hair out.

  10. AndrewHodges
    Member
    Posted 1 year ago #

    Tried including he global statement no difference.

  11. AndrewHodges
    Member
    Posted 1 year ago #

    I still cannot resolve this

    <?php
    query_posts($query_string.'&cat=-51');
    if (have_posts()) : while (have_posts()) : the_post();

    // The following determines what the post format is and shows the correct file accordingly'

    When I have cat=51 in the line only blogs of cat 51 appear when I have (minus)-51 all blogs appear including 51?

    Anyone any other suggestions?

  12. AndrewHodges
    Member
    Posted 1 year ago #

    I have cracked it!

    It is not in the code. The original blog was posted with Cat=43 that's was then changed to cat=51 (and only 51). The data base still has an entry for the previous version of the post as it would, but the query_posts() seems to return 43 and 51 which explains why it appears.

    I have written a rather poor sql script which returned both but I had assumed that query_posts would have been more sophisticated! Does this count as a bug?

Topic Closed

This topic has been closed to new replies.

About this Topic