Forums

set_query_var making it impossible to see Pages in Admin area (2 posts)

  1. sina94
    Member
    Posted 4 months ago #

    I've been working on a custom WP theme, and just noticed that I'm unable to see Pages in the Admin area. It loads Posts instead.

    After re-installing, deleting plugins, etc., I determined it was caused by a function I'd added to functions.php to dictate what is returned when a user searches on my blog.

    The offending code:

    //limit posts per search page & limit search to posts only
    
    function limit_posts_per_search_page() {
    	if ( is_search() )
    		set_query_var('posts_per_page', 10);
    		set_query_var('post_type','post');
    }
    add_filter('pre_get_posts', 'limit_posts_per_search_page');

    So, how do I modify this so that it filters searches on my blog, but doesn't keep me from seeing Pages in the Admin area?

  2. sina94
    Member
    Posted 4 months ago #

    I don't know that this is the best way to handle it, but this appears to work:

    function limit_posts_per_search_page() {
    	if ( is_search() ) {
    		if (! is_admin()) {
    			set_query_var('posts_per_page', 10);
    			set_query_var('post_type','post');
    		}
    	}
    }
    add_filter('pre_get_posts', 'limit_posts_per_search_page');

Reply

You must log in to post.

About this Topic