Skip Navigation

[Résolu] Query Multiple Post Types

This support ticket is created Il y a 5 années et 3 mois. There's a good chance that you are reading advice that it now obsolete.
Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 0 réponses, a 1 voix.

Dernière mise à jour par julieP Il y a 5 années et 3 mois.

Assisté par: Christian Cox.

Auteur
Publications
#1311451

I'm trying to display a message to a logged in user if that user has created 4 out of 4 specific post types. Is it possible to query 4 different post types in a single View? If not, are you able to assist with my attempts at creating shortcode in its place please? I'd like to be able to change it slightly to return 1 or 0 and then register it for use inside a wpv-conditional. Many thanks

I've tried creating the shortcode two ways but the message is displaying when it shouldn't (i.e. when not all conditions are met):-

add_shortcode('my_shortcode', 'ts1_my_shortcode');
function ts1_my_shortcode() {
    
    $user_id = get_current_user_id();    
    $msg = 'Thank you';
    
    $args1 = array(
            'post_type' => 'custom_type_one',
            'post_status' => 'draft',
            'post_author' => $user_id,
            'posts_per_page' => 1,
            );
        
    $exist1 = new WP_Query( $args1 );
    $numposts1 = $exist1->post_count;
    
    $args2 = array(
            'post_type' => 'custom_type_two',
            'post_status' => 'draft',
            'post_author' => $user_id,
            'posts_per_page' => 1,
            );
        
    $exist2 = new WP_Query( $args2 );
    $numposts2 = $exist2->post_count;
    
    $args3 = array(
            'post_type' => 'custom_type_three',
            'post_status' => 'draft',
            'post_author' => $user_id,
            'posts_per_page' => 1,
            );
        
    $exist3 = new WP_Query( $args3 );
    $numposts3 = $exist3->post_count;
    
    $args4 = array(
            'post_type' => 'custom_type_four',
            'post_status' => 'draft',
            'post_author' => $user_id,
            'posts_per_page' => 1,
            );
        
    $exist4 = new WP_Query( $args4 );
    $numposts4 = $exist4->post_count;
    
    
    if( ($numposts1 == 1) && ($numposts2 == 1) && ($numposts3 == 1) && ($numposts4 == 1) ) {
    return $msg;
    }
}
add_shortcode('my_shortcode', 'ts1_my_shortcode');
function ts1_my_shortcode() {
    
    $user_id = get_current_user_id();    
    $msg = 'Thank you';
    $type = array ('custom_type_one', 'custom_type_two', 'custom_type_three', 'custom_type_four');
    
    $args = array(
            'post_type' => $type,
            'post_status' => 'draft',
            'post_author' => $user_id,
            'posts_per_page' => 1,
            );
        
    $exist = new WP_Query( $args );
    $numposts = $exist->post_count;
    
    if ($numposts == 4) {
    return $msg;
   }
}