Amesey Amesey - 3 months ago 7
PHP Question

Display aside title if custom post type is related to course ID matching current course page ID

My current code below display the heading 'course instructors' at all times, when I need it to disappear when there are no course instructors.

What I need to happen is:


  • Query all 'trainers' posts.

  • Query the 'courses' field within each
    post.

  • Check to see if the current course page ID exists within the
    'courses' field within any of the trainers posts.

  • Display the
    trainers who have a course ID that matches the current course page
    ID.

  • If any trainers has course ID that matches current course page ID then display heading 'Course Instructor'.

    <div class="instructors">

    <?php
    $trainersArray = array(
    'post_type' => 'trainers',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC'
    );
    query_posts($trainersArray);

    $trainers = get_posts( $trainersArray );

    ?>

    <?php /*if($Course_ID):*/?>

    <h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show -->

    <?php /*endif;*/?>

    <div id="trainers_list">
    <?php

    foreach ( $trainers as $trainer ) :

    $trainerID = $trainer->ID;
    $trainer_courses = get_field('courses',$trainerID); //SELECT THE CONNECTED COURSE'S CUSTOM FIELD
    $fullName = get_the_title($trainerID); //GET THE NAME FIELD IN TESTIMONIAL POSTS
    $trainerPage = get_the_permalink($trainerID);
    $feedback_count = 0;

    if( $trainer_courses ):

    foreach( $trainer_courses as $trainer_course ):

    $trainerCourseID = $trainer_course->ID;

    if ($trainerCourseID == $Course_ID) : ?>

    <div class="instructor-block">
    <div class="instructor-profile ">
    <div class="profile-name">
    <?php echo $fullName; ?>
    </div>
    <div class="profile-link">
    <a href="<?php echo $trainerPage; ?>">
    View Full Profile
    </a>
    </div>
    </div>
    </div><br><!-- Another BR Fernando? and it's not even in body text, its after a DIV, whats your problem? -->
    <?php
    endif;

    endforeach;

    endif;

    endforeach; ?> <!--echo json_encode( $trainers ); -->

    <? wp_reset_query(); ?>

    </div><!-- .trainers_list -->
    </div><!-- .instructors -->


Answer

You need to perform the logic that checks if this course has any trainers before you can perform any checks on the result. Besides, moving you logic out of your html is a good idea. Wordpress doesnt exactly make this easy, but the least we can do is perform the logic at the top of the file:

<?php
//lets get all our logic out of the html
$trainersArray = array(
    'post_type'      => 'trainers',
    'posts_per_page' => -1,
    'orderby' => 'name',
    'order' => 'ASC'
);
query_posts($trainersArray);

$trainers = get_posts( $trainersArray );

//an array to hold any trainers for this specific course
$courseTrainers = [];

foreach ( $trainers as $trainer ) {

    $trainer_courses = get_field('courses',$trainer->ID);  //SELECT THE CONNECTED COURSE'S CUSTOM FIELD

    if(!$trainer_courses){
        continue; //no need to carry on this iteration
    }

    $fullName = get_the_title($trainer->ID); //GET THE NAME FIELD IN TESTIMONIAL POSTS
    $trainerPage = get_the_permalink($trainer->ID);

    foreach( $trainer_courses as $trainer_course ){
        //i presume $Course_ID is set elsewhere but is in scope?
        if($trainer_course->ID == $Course_ID){
            $courseTrainers[] = [
                'fullName'=>$fullName,
                'pageLink'=>$trainerPage
            ];
            break; //no further iterations needed
        }
    }
}
wp_reset_query();
?>
<div class="instructors">
    <?php if(count($courseTrainers)>0)://an empty array is falsey,but this is more explicit?>

        <h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show -->

    <?php endif;?>

    <div id="trainers_list">
        <?php foreach($courseTrainers as $trainer):?>
            <div class="instructor-block">
                <div class="instructor-profile ">
                    <div class="profile-name">
                        <?php echo $trainer['fullName']; ?>
                    </div>
                    <div class="profile-link">
                        <a href="<?php echo $trainer['pageLink']; ?>">
                            View Full Profile
                        </a>
                    </div>
                </div>
            </div><!-- Your BR has been eradicated Feranndo - long live css ;-)-->
        <?php endforeach;?>

    </div><!-- .trainers_list -->
</div><!-- .instructors -->

What follows are some other issues spotted before the main issue became clear, but are still worth keeping in the answer:

1: You are using short open tags, that are depreciated and are probably the issue, change:

  <? if (!$trainers) { }else{?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><? } ?>
//^short tag                                                                                                                           ^another

to:

<?php if (!$trainers) { }else{?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>

2: the double negative is weird, change to

<?php if ($trainers){?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>

3: $trainers is not declared until after the check, so move that code up:

<?php 

    $trainersArray = array(
       'post_type'      => 'trainers',
       'posts_per_page' => -1,
       'orderby' => 'name',
       'order' => 'ASC'
    );
    query_posts($trainersArray);

    $trainers = get_posts( $trainersArray );

?>
<div class="instructors">
    <?php if ($trainers){?><h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show --><?php } ?>
<div id="trainers_list">
    <?php                         
    foreach ( $trainers as $trainer ) {...

5: Finally, dont switch between if():endif; and if(){} syntax, its confusing. When working with html the former is prefered, so the final code becomes:

<div class="instructors">

    <?php
    $trainersArray = array(
        'post_type'      => 'trainers',
        'posts_per_page' => -1,
        'orderby' => 'name',
        'order' => 'ASC'
    );
    query_posts($trainersArray);

    $trainers = get_posts( $trainersArray );
    ?>

    <?php if($trainers):?>

    <h3 id="trainers_heading">Course Instructors</h3><!-- only show this if there is instructors to show -->

    <?php endif;?>

    <div id="trainers_list">
        <?php

        foreach ( $trainers as $trainer ) :

            $trainerID = $trainer->ID;
            $trainer_courses = get_field('courses',$trainerID);  //SELECT THE CONNECTED COURSE'S CUSTOM FIELD
            $fullName = get_the_title($trainerID); //GET THE NAME FIELD IN TESTIMONIAL POSTS
            $trainerPage = get_the_permalink($trainerID);
            $feedback_count = 0;

            if( $trainer_courses ):

                foreach( $trainer_courses as $trainer_course ):

                    $trainerCourseID = $trainer_course->ID;

                    if ($trainerCourseID == $Course_ID) : ?>
                        <div class="instructor-block">
                            <div class="instructor-profile ">
                                <div class="profile-name">
                                    <?php echo $fullName; ?>
                                </div>
                                <div class="profile-link">
                                    <a href="<?php echo $trainerPage; ?>">
                                        View Full Profile
                                    </a>
                                </div>
                            </div>
                        </div><br><!-- Another BR Feranndo? and it's not even in body text, its after a DIV, whats your problem? -->
                    <?php 
                    endif;

                endforeach; 

            endif; 

        endforeach; 
        wp_reset_query(); ?>

    </div><!-- .trainers_list -->
</div><!-- .instructors -->
Comments