Vince Mamba
Vince Mamba

Reputation: 187

Turn WP Query into ShortCode Wordpress

I have the following WP Query which works perfectly. Basically how it works is, on my website I have different areas of focus. When you click on one of the areas of focus for example math or science. All teachers associated with math or science will be displayed.

Here is the wp query

<?php $schools = $post->ID; // the current post id ?>

    <?php
    $args = array( 
        'post_type' => 'teacher',
        'meta_query' => array(
            array(
                'key'     => 'areas_of_focus',
                'value'   => $schools,
                'compare' => 'LIKE',
            ),
        ),
    );

    $schools_data_query = new WP_Query($args);

    ?>

    <?php

    if ( $schools_data_query->have_posts() ) {
        echo '<ul>';
        while ( $schools_data_query->have_posts() ) {
            $schools_data_query->the_post();
            //echo '<li>' . get_the_title() . '</li>';
            //echo '<li>' . get_permalink() . '</li>'; ?>        
           <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>

    <?php
        }
        echo '</ul>';
    } else {
        // no posts found
    }

Now I want to be able this into a shortcode. Here is what I have come up with, however it is not working. No matter what area of focus I click on the same teachers come up.

function list_teacher_shortcode($atts){
    global $post;
    $schools = $post->ID;
    $args = array( 
    'post_type' => 'teacher',
    'meta_query' => array(
        array(
            'key'     => 'areas_of_focus',
            'value'   => $schools,
            'compare' => 'LIKE',
        ),
    ),
);
    $schools_data_query = new WP_Query($args);
    global $post;
    $schools = $post->ID;
    $content = '';
    $content .= '<ul>';
        while($schools_data_query->have_posts()) : $schools_data_query->the_post();
        $content .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
        endwhile;
    $content .= '</ul>';
    wp_reset_query();
    return $content;
}
add_shortcode('list_teacher', 'list_teacher_shortcode');

I'm not very good at this backend programming but I'm assuming it has something to do with

global $post;

$schools = $post->ID;

I have it listed in two different areas but I tried removing it from the top and from the lower area and still get the same results.

Upvotes: 0

Views: 1054

Answers (1)

MurDaD
MurDaD

Reputation: 362

What you're using global $post; in your shortcode, it takes the last post on the page, so you must send post id in your shortcode

echo do_shortcode('[list_teacher postID="'.$post->ID.'"]');

And get it inside list_teacher_shortcode function.

 $a = shortcode_atts( array(
     'postID' => '',
 ), $atts );
 $postID = $a['postID'];

Then you don't need this code (you use it twice):

global $post;
$schools = $post->ID;

UPDATE

You can also use $query->the_post() and wp_reset_post_data() inside your shortcode. More information here https://codex.wordpress.org/Function_Reference/wp_reset_postdata

UPDATE 2 FULL CODE

Put it where you want to use shortcode

echo do_shortcode('[list_teacher postID="'.$post->ID.'"]');

Here is your full shortcode code

function list_teacher_shortcode($atts){
    $a = shortcode_atts( array(
        'postID' => '',
    ), $atts );
    $schools = $a['postID'];
    $args = array( 
    'post_type' => 'teacher',
    'meta_query' => array(
        array(
            'key'     => 'areas_of_focus',
            'value'   => $schools,
            'compare' => 'LIKE',
        ),
    ),
);
    $schools_data_query = new WP_Query($args);
    $content = '';
    $content .= '<ul>';
        while($schools_data_query->have_posts()) : $schools_data_query->the_post();
        $content .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
        endwhile;
    $content .= '</ul>';
    wp_reset_query();
    return $content;
}
add_shortcode('list_teacher', 'list_teacher_shortcode');

UPDATE 3

Also, you can use get_the_ID() description is here Then, you can remove attributes from the shortcode function and your first line of the function should look like this:

$school = get_the_ID();

Upvotes: 2

Related Questions