Support » Plugins » Hacks » PHP error Exclude post categories from loop

  • Hi, unsure if correct forum but here goes.

    I have an installation with Buddy theme installed with buddypress. I want to exclude a category of post from the loop and found this code at http://wpsites.net/web-design/exclude-category-posts-page/

    add_action( ‘pre_get_posts’, ‘exclude_category_posts’ );
    /**
    * @author Brad Dalton – WP Sites
    * @example http://wpsites.net/web-design/exclude-category-posts-page/
    */
    function exclude_category_posts( $query ) {
    if( $query->is_main_query() && $query->is_home() ) {
    $query->set( ‘cat’, ‘-27,-30’ );
    }
    }

    When I put this in my functions.php in my child theme, the admin outputs this error:

    Warning: Cannot modify header information – headers already sent by (output started at /home/mysite/public_html/wp-content/themes/buddy-child/functions.php:37) in /home/mysite/public_html/wp-admin/post.php on line 242

    Warning: Cannot modify header information – headers already sent by (output started at /home/mysite/public_html/wp-content/themes/buddy-child/functions.php:37) in /home/mysite/public_html/wp-includes/pluggable.php on line 1207

    The frontend is not affected, but shows the category ID I want to exclude.

    Can anyone please help me find out what the problem is?

    Terjemk

Viewing 6 replies - 1 through 6 (of 6 total)
  • My suspicion is that you have made some inadvertent changes to your child theme files, it is easy to do, as simple as adding a blank line at the end of a .php file after the ‘?>’ is enough to do this.

    There is suspicion about line 37 of functions.php, what is it ?

    Remove your changes to your “functions.php” file and see if the error goes away, then add an empty function body, then the add_action call, then the internals of the function.

    Thread Starter terjemk

    (@terjemk)

    @rossmitchell

    Great! Thanks. That was the issue. I had two blank lines at the end after ‘?>’.

    Lesson learned!

    BUT, although the site now is up, the category still shows in the loop, showing up in the widgets showing latest post.

    Any idea why?

    BUT, although the site now is up, the category still shows in the loop,

    Even though your code looks a lot like an example on this page:
    https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
    Several questions come to mind:

    Is your filter being fired ? Put code in the function that increments a global counter, report the counter value in the page footer.

    Are you using the filter as intended ? Google is your friend. Even though the example looks a lot like yours I would also look for more examples.

    Are the tests: $query->is_main_query() $query->is_home() working ? Once again log the results in the footer.

    Thread Starter terjemk

    (@terjemk)

    Ok thanks for your reply! I really appreciate it, but I think this is getting a bit to technical for me, I have just startet to look into php code/hacks.

    I might look more into it later on, but again, thanks for your reply.

    Terjemk

    the category still shows in the loop, showing up in the widgets showing latest post.

    ‘pre_get_posts’ is only meant for the main query, not for custom queries and loops in widgets.

    what widget are you using?
    is that from a plugin?

    Not so sure that limiting it to the main loop is appropriate or necessary, certainly the test: $query->is_main_query() would seem to make it so, is there a different test for “is_widget()” ?

    Suggest that you remove the tests and ALWAYS exclude your categories, see if that makes your widgets look right. If so then sort out which test are needed to make it specific to widgets.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘PHP error Exclude post categories from loop’ is closed to new replies.