skip to Main Content

code original

if ( 'property' != $post->post_type ) {
        return $query;
    }

what i want to do but it doesn’t work like that

 if ( 'property' != $post->post_type ) {
 if ( 'testimonial' != $post->post_type ) {
            return $query;
        }

How to add 2 or 3 post types ?

I add the full code so you can maybe understand why it doesn’t work
and figure out how to make it work for different post types

add_filter( 'ajax_query_attachments_args', 'filter_query_attachments_args' );

function filter_query_attachments_args( $query ) {
    // 1. Only users with access
    if ( ! current_user_can( 'upload_files' ) ) {
        wp_send_json_error();
    }

    // 2. No manipulation for admins.
    // After all they have access to all images.
    //if ( current_user_can( 'administrator' ) ) {
        //return $query;
    //}

    // 3. No images, if the post_id is not provided
    if ( ! isset( $_REQUEST['post_id'] ) ) {
        wp_send_json_error();
    }

    // 4. No images, if you are not the post type manager or author
    $post = get_post( (int) $_REQUEST['post_id'] );
    if ( ! $post instanceof WP_Post ) {
        return $query;
    }

    // 5. You can also restrict the changes to your custom post type
    // Only filter for our custom post types
    if ( 'property' != $post->post_type ) {
        return $query;
    }
    
    // 5. You can also restrict the changes to your custom post type
    if ( 'testimonial' != $post->post_type ) {
        return $query;
    }
    
    // 8. Don't show private images
    $query['post_status'] = 'inherit';

    // 9. Filter to display only the images attached to the post
    $query['post_parent'] = $post->ID;

    // 10. Filter to display only the user uploaded image
    $query['editor'] = $current_user->ID;

    return $query;
}

2

Answers


  1. You can use in_array

    if (in_array($post->post_type, ['property', 'testimonial'])) {}
    
    
    Login or Signup to reply.
  2. If I understand what you are asking, you want to be able to check two separate variables from the contents of post. You can put them in the same if block like this:

    if ( 'property' != $post->post_type && 'testimonial' != $post->post_type ) {
             return $query;
        }
    

    The two equality comparisons need to be separated by a logical && operator.

    Alternatively, you could nest the if blocks like this:

    if ( 'property' != $post->post_type) {
        if ('testimonial' != $post->post_type ) {
             return $query;
        }
    }
    

    Depending on your implementation, you may want to use !== instead of != to check not only for the value but also the type. See https://www.php.net/manual/en/language.operators.comparison.php
    for more information about operators in PHP.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search