1

So I have a pretty in-depth shortcode with attributes that displays Staff members based on the attributes set for Location (post type) and Specialty (post type).

I'm having an issue where if I place two shortcodes on the same page with different locations (see below) it displays the Staff for the 1st shortcode perfectly, but the 2nd shortcode doesn't display the staff and instead displays the fallback I've coded.

I thought I needed to use wp_reset_postdata() but I never set wp_query.

Hopefully, you can guide me in the right direction. Thank you.

Shortcodes look like this:

  1. [staff_members location="carlsbad" type="doctor" specialty="emergency / critical care"]

  2. [staff_members location="ontario" type="doctor" specialty="emergency / critical care"]

Here is my PHP:

/* ------- List Staff by Location/Type/Specialty/ for Specialty/Location/Staff Pages - Shortcode -------- */
add_shortcode( 'staff_members', 'staff_members_shortcode' );
function staff_members_shortcode( $atts ) 
{
extract( shortcode_atts( array(   //Set parameters the shortcode can use
        'location' => 'location',
        'type' => 'type',
        'specialty' => 'specialty',
        'style' => 'style'
), $atts ));

if( $location ) $location_id = get_page_by_title( $location, OBJECT, 'locations' )->ID; //If location parameter is present use the parameter value to get the ID of the location page of the same name.

if( $specialty ) $specialty_id = get_page_by_title( $specialty, OBJECT, 'specialties' )->ID; //If specialty parameter is present use the parameter value to get the ID of the specialty page of the same name.


// Set the Meta keys and Orderby parameters for Doctors 
if ('doctor'==$type) 
{
    $meta_keys = array(
        'work_title_clause' => array(
            'key' => 'work_title',
        ),
        'last_name_clause' => array(
            'key' => 'last_name',
        ),
    );

    $orderby = array(
        'work_title_clause' => 'ASC',
        'last_name_clause' => 'ASC',
    );
}


// Set the Meta keys and Orderby parameters for Vet Techs   
if ('vet_tech'==$type) 
{
    $meta_keys = array(
        'work_title_clause' => array(
            'key' => 'work_title',
        ),
        'supervisor_clause' => array(
            'key' => 'supervisor',
            'type' => 'NUMERIC',
        ),
        'last_name_clause' => array(
            'key' => 'last_name',
        ),
    );

    $orderby = array(
        'supervisor_clause' => 'DESC',
        'work_title_clause' => 'ASC',
        'last_name_clause' => 'ASC',
    );
}


// Set the Meta keys and Orderby parameters for Front Desk  
if ('front_desk'==$type) 
{
    $meta_keys = array(
        'work_title_clause' => array(
            'key' => 'work_title',
        ),
        'supervisor_clause' => array(
            'key' => 'supervisor',
            'type' => 'NUMERIC',
        ),
        'last_name_clause' => array(
            'key' => 'last_name',
        ),
    );

    $orderby = array(
        'supervisor_clause' => 'DESC',
        'work_title_clause' => 'ASC',
        'last_name_clause' => 'ASC',
    );
}       



// Set the Meta keys and Orderby parameters for Admins  
if ('admin'==$type) 
{
    $meta_keys = array(
        'admin_order_clause' => array(
            'key' => 'admin_order',
            'type' => 'NUMERIC',
            ),
    );
    $orderby = array(
        'admin_order_clause' => 'ASC',
    );
}

/* ------------------------- */
/*     Start get_posts()     */
/* ------------------------- */

$staff = get_posts(array(
    'post_type' => 'staff',
    'nopaging' => true,
    'meta_query' => array(
        $meta_keys,
        'relation' => 'AND',
        array(
            'key' => 'location', // name of custom field
            'value' => $location_id,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'type', // name of custom field
            'value' => $type, 
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'specialties', // name of custom field
            'value' => $specialty_id,
            'compare' => 'LIKE'
        ),
    ),
    'orderby' => $orderby,
));

$locations = get_posts(array(
    'post_type' => 'locations',
    'nopaging' => true,
    'orderby' => 'name',
    'order' => 'ASC',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'specialties', // name of custom field
            'value' => $specialty_id,
            'compare' => 'LIKE'
        ),
    )
));

if( $staff ): 
{
    if($style=='style') $style = 'doctor';
    foreach ($staff as $staff_member) 
    {

        $staff_member_id = $staff_member->ID;
        $thumb_staff = wp_get_attachment_image_src( get_post_thumbnail_id($staff_member_id), 'full' );

        //This is the div soup for a single staff member layout
        $staff_html='<div class="'.$style.'-wrapper">
            <div class="'.$style.'-img" style="background-image: url('.$thumb_staff['0'].')"></div>
            <div class="'.$style.'-name">'.get_the_title( $staff_member_id ).'</div>
            <div class="'.$style.'-title"> '.get_field( 'work_title', $staff_member_id ).'</div>
              </div>';

        // If the Type of Staff post is 'doctor', wrap the query in a link to that doctor's page.
        // This stops all staff post from being linked since we only want to link doctors.
        if ( $type == 'doctor' ) $staff_html = '<a href="'.get_permalink($staff_member_id).'">'.$staff_html.'</a>';
        $return_string.=$staff_html;
    }
}


elseif( $locations ): 
{
    foreach ($locations as $location) 
    {
        $location_id = $location->ID;

        $other_location='<div class="w-btn-wrapper align_left"><a class="w-btn style_solid color_primary icon_none" href="'.get_permalink($location_id).'">Available at'.' '.get_the_title( $location_id ).'</a></div>';
        $return_string.=$other_location;
    }
}


elseif( !$locations ):
{
    $return_string='No Doctor or Location is labeled for this Specialty';
}
endif;

return '<div class="'.$style.'-container">'.$return_string.'</div>';
}
/* --- END SHORTCODE FUNCTIONS FOR DYNAMIC CUSTOM POST TYPE CONTENT --- */
  • Got it sorted, Visual Composer was running a bad shortcode between my custom shortcode instances. – Michael H Jan 31 '17 at 0:16

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.