• Resolved tiaanswart

    (@tiaanswart)


    Hi

    Does anyone know id there are any Widget Booleans in WP_Query?

    I need to exclude some post types from the a recent posts widget but need to include those same post types in the main query for the front page…

    I am using the pre_get_posts filter…

    function all_archive($query) {
        if( is_admin() || isset($query->query_vars['post_type']) && 'nav_menu_item' == $query->query_vars['post_type'] ) {
            return;
        } elseif( is_archive() || is_tag() || is_category() || is_author() || is_year() || is_month() || is_day() || ( is_home() && is_main_query() ) ) {
            $post_type = get_query_var('post_type');
    	    if($post_type) {
    	        $post_type = $post_type;
    	    } else {
    	        $post_type = array('post','widget');
    	    }
            $query->set('post_type',$post_type);
    	    return $query;
        }
    }
    add_filter('pre_get_posts', 'all_archive');

    Any help would greatly be appreciated ??

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    You can add a filter to the widget’s query:

    add_filter( 'widget_posts_args', 'recent_posts_widget_post_types' );
    
    function recent_posts_widget_post_types( $args ) {
    
    	// set the post types for the recent post widget
    	$args['post_type'] = array( 'post', 'movie' );
    
    	return $args;
    }

    Use conditional tags in the function to only set the post types on specific pages:
    https://codex.www.ads-software.com/Conditional_Tags

    Thread Starter tiaanswart

    (@tiaanswart)

    Thanks for pointing out this filter! However sadly it won’t work…

    The pre_get_posts filter is run after the widget_posts_args filter…

    So the pre_get_posts filter overwrites the changes the widget_posts_args filter has already made…

    ??

    Thread Starter tiaanswart

    (@tiaanswart)

    I did a bit of testing now and the issue seems to not happen on page templates with a query_posts() function?…

    This is just a little too weird for me…

    Moderator keesiemeijer

    (@keesiemeijer)

    I’m not sure I understand. The recent post widget does a seperate query that is not influenced by ‘pre_get_post’.
    What is it exactly you want to do, and on what page?

    Thread Starter tiaanswart

    (@tiaanswart)

    My apologies for the late reply… For now I have implemented a hack to temporarily resolve issue.

    What I am trying to accomplish is through pre_get_posts filter change the query post type on the Home page (and some other templates as well see conditional tags in 1st post). The main query works as expected and retrieves all posts for the post types however the recent posts widget in the footer sidebar also shows widget posts now.

    The widget_posts_args filter does not help with this issue as the args for the widget query is filtered before pre_get_posts filters it again, thus adding the widget post type to the query args.

    Moderator keesiemeijer

    (@keesiemeijer)

    The widget is doing a secondary query with new WP_Query that normally does not get filtered. Maybe you have to alter your pre_get_posts action to only add the ‘widget’ post type for the main query when on the home page. Here is an example:

    function all_archive( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
      	// alter the main query on home page
        if(is_home()){
           $query->set('post_type', array('post','widget') );
        }
        // do stuff for other pages
    
      }
      // this is an action. There's no need to return anything
    }
    add_action( 'pre_get_posts', 'all_archive' );

    Thread Starter tiaanswart

    (@tiaanswart)

    Thanks for the solution. I tried implementing your solution and it still did not work. Somehow the pre_get_posts filter is overriding the other filters. I will stick with the hack until I have more time to come up with a more permanent solution.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WP Query Widget Boolean’ is closed to new replies.