1

I'm a beginner when it comes to wordpress development...

I have an user role "owner". Those own "machines" (custom post type) I want owners in the admin backend only to see their own machines (those where the owner is the author) and the machines owned by the same company (taxonomy for the machines). The company is a user's meta field and it should match with the post's taxonomy.

For the condition "same company", I change the query to only filter the posts with a given company-taxonomy (see "abc").

How can I extend the query so that users also see their own posts even if those don't have the company field filled ?

Goal: query(same company) OR query(same author)

function only_own_company( $query ) {
    global $typenow;
    $current_user = wp_get_current_user();
    $post_type = 'machine'; // change to your post type
    $role = 'owner';

    if ( $typenow == $post_type && current_user_can( $role ) ) {
        $query->set( 'tax_query', array(
            array(
                'taxonomy' => 'company',
                'field'    => 'slug',
                'terms'    => 'abc',
            ),
        ) );
    }
}
add_action( 'pre_get_posts', 'only_own_company' );

I tried to combine it with a second new query but that doesn't seem to be the right way...

$query2 = new WP_Query( array( 'author', $current_user->ID ) );
$query->posts = array_merge( $query->posts, $query2->posts );
  • You can't merge queries WP_Query objects like that – Tom J Nowell Aug 23 '17 at 14:29
  • As a sidenote, don't forget the 4th argument in the add_action() which defines the number of arguments to pass to the callback function. Without that, your callback can't access to the $query. See my answer. – ClemC Aug 24 '17 at 10:30
0

See WP_Query as reference for the post query filters. You can filter posts by their authors with the 'author' query var as you filter posts by their taxonomy with 'tax_query' query var.

Then, conditionally set the 'tax_query' query var depending on the user's company meta field. If it's empty, set the 'author' query var instead.

add_action( 'pre_get_posts', 'alter_query', 99, 1 );

function alter_query( $query ) {
    global $typenow;

    if ( 'machine' === $typenow && current_user_can( 'owner' ) ) {
        $user    = wp_get_current_user();
        $company = get_user_meta( $user->ID, 'company', true );

        if ( '' !== $company ) {
            $query->set( 'tax_query', array(
                array(
                    'taxonomy' => 'company',
                    'field'    => 'slug',
                    'terms'    => sanitize_key( $company ),
                ),
            ) );

        } else {
            $query->set( 'author', $user->ID );
        }
    }
}

Note: sanitize_key() might be necessary depending on how you store company user's meta value. It lowercases the string and replaces in it the blank spaces by hyphens. This is used by WP for example to define post's slug from the post's title - In other words, it "slugify" a string...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.