WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] pre_get_posts category filter removes custom menu items (4 posts)

  1. rebelit
    Member
    Posted 2 years ago #

    I have a frontpage which shows a different category of posts depending on whether a user is logged in or not.

    using the following code in pre_get_posts()

    function lg_set_category_filter($wp_query) {
    	if(is_front_page()) {
    		if (is_user_logged_in()) {
    			$wp_query->set('cat','6');
    		}
    		else {
    			$wp_query->set('cat','5');
    		}
    	}
    
     }

    Now, if I also display a custom menu widget on that page the menu is empty. It seems the query set is interfering with the custom menu widget.

    How can I stop it removing custom menu entries?

  2. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    think we need to check for is_main_query.... (my syntax may be off... copied that from my own template)

    if( $query->is_main_query() && $query->is_front_page() ) {

    EDIT: here's a better explanation than I could give... had to find the link!
    http://www.billerickson.net/customize-the-wordpress-query/

  3. rebelit
    Member
    Posted 2 years ago #

    Worked perfectly,
    Thanks!

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    Excellent!! Thanks for marking the thread resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic