$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - I am trying to hide a custom post type category to logged in users with Pre_Get_Posts|Concepts Of Algorithm

categories - I am trying to hide a custom post type category to logged in users with Pre_Get_Posts

admin2025-01-08  8

I have a custom post type in my theme called portfolio. It's categories are called project-categories. I am trying to exclude a project category for logged in users. I cannot get this to work at all. I have read through the pre_get_posts codex but I am just stuck. I'm not sure if I am defining the categories for my custom post type or if it would be the same as a normal post category. When I click on the category to edit it, this is in the browser url bar

wp-admin/edit-tags.php?action=edit&taxonomy=project-type&tag_ID=94&post_type=po‌​rtfolio

Here is the code I'm trying right now.

function exclude_category( $query ) {
    if ( is_user_logged_in() && $query->is_main_query() ) {
        $taxquery = array(
        'taxonomy' => 'project-type',
        'field' => 'id',
        'terms' => array( 94 ),
        'operator' => 'NOT IN'
    );

        $query->set( 'tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Any ideas on what I might be doing wrong would be greatly appreciated. Thanks

I have a custom post type in my theme called portfolio. It's categories are called project-categories. I am trying to exclude a project category for logged in users. I cannot get this to work at all. I have read through the pre_get_posts codex but I am just stuck. I'm not sure if I am defining the categories for my custom post type or if it would be the same as a normal post category. When I click on the category to edit it, this is in the browser url bar

wp-admin/edit-tags.php?action=edit&taxonomy=project-type&tag_ID=94&post_type=po‌​rtfolio

Here is the code I'm trying right now.

function exclude_category( $query ) {
    if ( is_user_logged_in() && $query->is_main_query() ) {
        $taxquery = array(
        'taxonomy' => 'project-type',
        'field' => 'id',
        'terms' => array( 94 ),
        'operator' => 'NOT IN'
    );

        $query->set( 'tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Any ideas on what I might be doing wrong would be greatly appreciated. Thanks

Share Improve this question asked Apr 29, 2014 at 6:36 Musik8101Musik8101 1112 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

OK, so you have few mistakes in there.

  1. field can be term_id or slug, but not id.
  2. tax_query should be array of arrays and not an array.

So here is fixed code:

function exclude_category( $query ) {
    if ( is_user_logged_in() && $query->is_main_query() ) {
        $taxquery = array(
            array(
                'taxonomy' => 'project-type',
                'field' => 'term_id',
                'terms' => array( 94 ),
                'operator' => 'NOT IN'
            ),
        );

        $query->set( 'tax_query', $taxquery);
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

It's not tested, but I'm pretty sure it should work just fine.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267124a1175.html

最新回复(0)