$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'); ?>custom post types - WP Query Conditionally query meta and taxonomy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom post types - WP Query Conditionally query meta and taxonomy

matteradmin8PV0评论

I develop a plugin which includes a custom post type that is displayed as a list within WP Admin.

There are varying levels of users defined within the plugin and a few other options which determine which of the posts are displayed to the currently logged in user. Additionally, posts may belong to a custom taxonomy. In this case, further conditions should determine which posts the current user sees. For example, perhaps the current user should be able to see all posts with the term Support from the Department taxonomy.

I am hooking into pre_get_posts in order to build out the query ensuring that the current user only sees the posts they should. Admins see all, others only see posts whereby they are assigned. This is all working fine with standard meta queries.

It's the taxonomy I'm having an issue with. I understand it is not possible to create a WP Query to query posts where a meta key exists and equals a value OR the post has a specific taxonomy term associated with it, unless I build out a database query of my own.

What is the best way to do this ensuring I keep the DB queries minimal?

I've posted below my current query. I'm looking for guidance on the best hook to use and query to use in order to achieve what I need.

function kbs_restrict_agent_ticket_view( $query )   {

    if ( ! is_admin() || 'kbs_ticket' != $query->get( 'post_type' ) )   {
        return;
    }

    $admin_agents = kbs_get_option( 'admin_agents' );

    if ( empty( $admin_agents ) && current_user_can( 'administrator' ) )    {
        $query->set( 'p', '99999999' );
        return;
    }

    // If user is admin and admins are agents, they see all.
    if ( current_user_can( 'manage_ticket_settings' ) ) {
        return;
    }

    if ( kbs_get_option( 'restrict_agent_view', false ) )   {
        $agent_id = get_current_user_id();

        $meta_query = array(
            'relation' => 'OR',
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => $agent_id,
                'type'    => 'NUMERIC'
            ),
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => ''
            ),
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => 'anything',
                'compare' => 'NOT EXISTS'
            )
        );

        if ( kbs_multiple_agents() )    {
            $meta_query[] = array(
                'key'     => '_kbs_ticket_agents',
                'value'   => sprintf( ':%d;', $agent_id ),
                'compare' => 'LIKE'
            );
        }

        $query->set( 'meta_query', $meta_query );

  }

} // kbs_restrict_agent_ticket_view
add_action( 'pre_get_posts', 'kbs_restrict_agent_ticket_view' );

I'm effectively now looking for OR post has term ('support', 'department')

I develop a plugin which includes a custom post type that is displayed as a list within WP Admin.

There are varying levels of users defined within the plugin and a few other options which determine which of the posts are displayed to the currently logged in user. Additionally, posts may belong to a custom taxonomy. In this case, further conditions should determine which posts the current user sees. For example, perhaps the current user should be able to see all posts with the term Support from the Department taxonomy.

I am hooking into pre_get_posts in order to build out the query ensuring that the current user only sees the posts they should. Admins see all, others only see posts whereby they are assigned. This is all working fine with standard meta queries.

It's the taxonomy I'm having an issue with. I understand it is not possible to create a WP Query to query posts where a meta key exists and equals a value OR the post has a specific taxonomy term associated with it, unless I build out a database query of my own.

What is the best way to do this ensuring I keep the DB queries minimal?

I've posted below my current query. I'm looking for guidance on the best hook to use and query to use in order to achieve what I need.

function kbs_restrict_agent_ticket_view( $query )   {

    if ( ! is_admin() || 'kbs_ticket' != $query->get( 'post_type' ) )   {
        return;
    }

    $admin_agents = kbs_get_option( 'admin_agents' );

    if ( empty( $admin_agents ) && current_user_can( 'administrator' ) )    {
        $query->set( 'p', '99999999' );
        return;
    }

    // If user is admin and admins are agents, they see all.
    if ( current_user_can( 'manage_ticket_settings' ) ) {
        return;
    }

    if ( kbs_get_option( 'restrict_agent_view', false ) )   {
        $agent_id = get_current_user_id();

        $meta_query = array(
            'relation' => 'OR',
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => $agent_id,
                'type'    => 'NUMERIC'
            ),
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => ''
            ),
            array(
                'key'     => '_kbs_ticket_agent_id',
                'value'   => 'anything',
                'compare' => 'NOT EXISTS'
            )
        );

        if ( kbs_multiple_agents() )    {
            $meta_query[] = array(
                'key'     => '_kbs_ticket_agents',
                'value'   => sprintf( ':%d;', $agent_id ),
                'compare' => 'LIKE'
            );
        }

        $query->set( 'meta_query', $meta_query );

  }

} // kbs_restrict_agent_ticket_view
add_action( 'pre_get_posts', 'kbs_restrict_agent_ticket_view' );

I'm effectively now looking for OR post has term ('support', 'department')

Share Improve this question asked Feb 15, 2019 at 10:07 MikeMike 4473 silver badges12 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If it's not too much of work, you could switch from using meta data to custom (private) terms for tagging the tickets to agents.

So when ever an agent (user) is created, you would have custom function run that executes wp_insert_term to create an agent-term, where the newly created user ID is used as the term slug. You would then use wp_set_object_terms to attach the agent-term to the ticket posts. And when using wp_set_object_terms you need to make sure agent/user ID is passed as string as the functions $terms parameter. If you try passing the user ID as int WP will interpret it is a term ID and setting the terms would fail.

By using agent-terms you could avoid using meta_query and do everything with tax_query.

Post a comment

comment list (0)

  1. No comments so far