1

I am trying to embed a custom WP_Query inside a shortcode I have activated on one of my pages (created in Elementor). I have a Custom Post Type called 'inspector-profiles', and for some reason when I add that to my $args array, it gets either not honored by WP, or it is overwritten by something else.

I cannot see what is causing an overwrite if anything.

The code is this, so far:

private function isInspector() {
    if (is_user_logged_in()) {
        $user = wp_get_current_user();
        return in_array('general_inspector', (array)$user->roles);
    }
    return false;
}

public function sc_inspectorProfileData() {
    global $wpdb;
    $user = \wp_get_current_user();


    // WP_Query arguments
    $args = array(
        'post_type'              => 'inspector-profiles',
        'post_status'            => 'publish',
        'author'                 => $user->ID
    );


    // The Query
    echo '<pre>';

    $myq = new \WP_Query( $args );

    print_r($myq->request);

    if ($myq->have_posts()) {
        while ($myq->have_posts()) {
            $myq->the_post();
            print_r($myq->post);
            wp_reset_postdata();
            wp_reset_query();
        }
    }

    echo '</pre>';
}

When I create the arguments for WP_Query above, the $args array looks fine prior to running the query. When I run the query, I immediately spit out the generated SQL query, and it looks like this:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_author IN (1) AND wp_posts.post_type IN ('post', 'page', 'attachment', 'memberpressproduct', 'memberpressgroup', 'mpdl-file', 'ht_kb') AND ((wp_posts.post_status = 'publish')) ORDER BY wp_posts.post_date DESC LIMIT 0, 200

My specified post_type is not even in that list. What am I doing wrong? I have tried using instances of get_posts(), using the WP_Query object, and some stuff that has been deprecated even.

Your Answer

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

Browse other questions tagged or ask your own question.