Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

php add 2 or more post_type in $query

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
}

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading