• Resolved khanselman

    (@khanselman)


    I’m using a plugin to create an issue based publication. The plugin creates a custom taxonomy issuem_issue. I’m trying to filter the category page to only show the active issue (using their built in function get_active_issuem_issue()).

    I’m added the following to the theme’s function.php file:

    function limit_issuem_taxonomies_to_active_issue( $query ) {
        if ( is_admin() || !($query->is_main_query()) ){
            return;
    	}
        if ( is_category() ) {
    		if ( function_exists( 'get_active_issuem_issue' ) ) {
    			$tax_query = array(
    				array(
    					'taxonomy' => 'issuem_issue',
    					'field' => 'slug',
    					'terms' => get_active_issuem_issue(),
    					)
    				);
    			$query->set( 'tax_query', $tax_query );
    		}
            return;
        }
    }
    add_action( 'pre_get_posts', 'limit_issuem_taxonomies_to_active_issue', 1 );

    This function works while I’m logged in, but once I log out, the category page shows all articles in all the issues, not just the current active issue.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Do you have a snippet of the function get_active_issuem_issue()?

    Thread Starter khanselman

    (@khanselman)

    From the plugins’ functions:

    function get_active_issuem_issue() {
        if ( !empty( $_COOKIE['issuem_issue'] ) )
            return $_COOKIE['issuem_issue'];
        else if ( !empty( $_GET['issue'] ) )
            return $_GET['issue'];
        else
            return get_issuem_issue_slug();
    }
    
    function get_issuem_issue_slug( $id = false ) {
        if ( !$id ) {
            $issue = get_term_by( 'id', get_newest_issuem_issue_id(), 'issuem_issue' );
        } else {
            $issue = get_term_by( 'id', $id, 'issuem_issue' );
        }
        return ( ( is_object( $issue ) && !empty( $issue->slug ) ) ? $issue->slug : '' );
    }

    ummm. I a not seeing anything from this code that could be the issue BUT Something does stick out and is just a thought.

    If for any reason get_active_issuem_issue () does not return anything and you still use the tax_query it will return all posts. Since I can only see snippets of the plugin I would start looking at the function get_active_issuem_issue () itself. If it changes when logged in or not then backtrace from there.

    Thread Starter khanselman

    (@khanselman)

    I did a print_r on $query when logged in and logged out, and $query is identical and is putting the correct issue in the tax_query term. I’m not seeing anything that would result in it only working when logged in.

    Are you using a plugin for any kind of membership? I am asking the obvious because if the query is identical as you are saying, then the results should be identical.

    Thread Starter khanselman

    (@khanselman)

    No plugin for membership. That’s why I am so confused. Unless the custom taxonomy is only available to admin, I can’t figure out why it’s not filtering the results. It makes no sense.

    Moderator keesiemeijer

    (@keesiemeijer)

    Also try using a higher priority for the action:

    add_action( 'pre_get_posts', 'limit_issuem_taxonomies_to_active_issue', 99 );

    Other plugins or your theme could alter the query with pre_get_posts after your plugin with such a low priority.

    Thread Starter khanselman

    (@khanselman)

    Wow.. Keesiemeijer, Thank you! For some reason I was under the impression that the lower the number the more importance was given to that action. I changed it to 100 and it worked perfectly. Thank you!

    Nice ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘pre_get_posts only works when logged in’ is closed to new replies.