Sunday, February 5, 2023
HomeWordPress Developmentwp question - Unliftable WP_Query breaks web site

wp question – Unliftable WP_Query breaks web site


I am utilizing WP_Query to show posts of sort ‘programs’ based mostly on enrollment information contained in posts of sort ‘tutor_enrolled’. The corresponding submit IDs are collected in $course_ids. As quickly as I publish the present question, my web site goes unresponsive, supposedly as a result of complexity of the question (or maybe, as a result of some syntax error). Is there any strategy to keep away from this question breaking the web site?

perform custom_query_callback() {

$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'post_author' => $current_user->ID,
'fields' => 'ids',
'post_parent' => 0
);

$enrollments = new WP_Query( $args );

if ( $enrollments->have_posts() ) {
    $course_ids = array();
    whereas ( $enrollments->have_posts() ) {
        $enrollments->the_post();
        $course_ids[] = get_post_field( 'post_parent' );
    }
    wp_reset_postdata();
    
    $args = array(
    'post_type' => 'programs',
    'post__in' => $course_ids,
    'posts_per_page' => -1,
    'orderby' => 'post__in'
    );
    $programs = new WP_Query ( $args );
    
    if ( $courses->have_posts() ) {
        whereas ( $courses->have_posts() ) {
            $courses->the_post();
        }
        wp_reset_postdata();
        }
    }
}

add_action( 'elementor/question/custom_query_callback', 'custom_query_callback' );

I used to be pondering of simplifying it within the course of:

perform custom_query_callback( $question ) {

$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'post_author' => $current_user->ID,
'fields' => 'ids',
'post_parent' => 0
);

$enrollments = new WP_Query( $args );

if ( $enrollments->have_posts() ) {
    $course_ids = array();
    whereas ( $enrollments->have_posts() ) {
        $enrollments->the_post();
        $course_ids[] = get_post_field( 'post_parent' );
    }
    wp_reset_postdata();
    
    }
$query->set( 'post__in', $course_ids );
}

add_action( 'elementor/question/custom_query_callback', 'custom_query_callback' );

Nevertheless, I’m not very conversant in WP_Query on the whole and imagine that my formatting could be very improper. Can somebody recommend me the most effective (and correct by way of WP_Query) strategy to obtain this?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments