WordPress.org

Forums

[resolved] Problem: WP_Query always show all posts instead of from just one category (2 posts)

  1. Joost
    Member
    Posted 4 years ago #

    I've got a problem with WP_Query and I was wondering if someone could help.

    I'm registering a widget in the functions.php file of my theme, but the WP_Query command keeps returning all posts, while I am specifying a category. This is the code:

    add_action('init','initTVwidget');
    function initTVwidget() {
    	if ( function_exists('register_sidebar_widget') )
    		register_sidebar_widget(__('TV and player'), 'widget_tvAndCartoonPlayer');
    }
    
    function widget_tvAndCartoonPlayer() {
    	$blog_posts = new WP_Query('showposts=2&cat=25');
    	print_r($blog_posts);
    }

    Is there a scope problem? Because when I place the same WP_Query code snippet outside of the function in the functions.php it works.

  2. Joost
    Member
    Posted 4 years ago #

    I solved it. One of the plugins I used added a pre_get_posts filter that changed the category selection again.

    Fix.

Topic Closed

This topic has been closed to new replies.

About this Topic