0

I'm trying to get meta information from the first four posts that have a meta key equal to Sedan, then format that posts meta in order to set up gallery type display boxes. I believe it has something to do with my method, as I am still on the steep part of the learning curve. Any comments, advice or answers would be deeply appreciated.

With this code, the HTML I'm trying to generate does not show on the front-end. As if I hadn't done anything.

add_shortcode('sedan-tabs','build_sedan_tabs');
function build_sedan_tabs(){
    $args1 = array(
        'meta_key'   => 'vehicle-type',
        'meta_value' => 'Sedan',
        'posts_per_page' => '4'
    );
    $query1 = new WP_Query($args1);
    while ( $query1->have_posts() ) {
        $query1->the_post();
        $currentPostID = get_the_id();
        $querySedans =  '<div class="col-sm-3><div class="display-car"><img src="' . get_post_meta(get_the_id(),'wpcf-vehicle-images', true) . '"/>' . 
        '<h3>' . get_post_meta($currentPostID,'wpcf-vehicle-year', true) . ' ' . 
        get_post_meta($currentPostID,'wpcf-vehicle-make', true) . ' ' . 
        get_post_meta($currentPostID,'wpcf-vehicle-model', true) .'</h3><p>' .
        get_post_meta($currentPostID,'wpcf-vehicle-mileage', true) . ' ' . 
        get_post_meta($currentPostID,'wpcf-vehicle-transmission', true) . '<br /><strong>' . 
        get_post_meta($currentPostID,'wpcf-vehicle-retail-price', true) . '</strong></p></div></div><a href="' . get_permalink($currentPostID, false) . '"></a>';
        return get_the_id();
    }
}
1

Your have two major issues here

  • The string you are building is correct, but it gets overriden on each iteration of the loop, which at the end will only hold the info from the last post. You need to define your variable outside the loop and then concatenate your info to that variable across the entire loop in order for you to display all postdata from all posts

  • You are returning the wrong info. You are returning post id after the first iteration of the loop. Your should be returning your string with post info outside the loop

I think your code should look like this:

add_shortcode('sedan-tabs','build_sedan_tabs');
function build_sedan_tabs(){
    $querySedans = '';
    $args1 = array(
        'post_type'  => 'inventory',
        'meta_key'   => 'vehicle-type',
        'meta_value' => 'Sedan',
        'posts_per_page' => '4'
    );
    $query1 = new WP_Query($args1);
    while ( $query1->have_posts() ) {
        $query1->the_post();
        $currentPostID = get_the_id();
        $vehicleImg = get_post_meta($currentPostID,'vehicle-images', true);
        $querySedans .=  '<div class="col-sm-3"><div class="display-car"><img src="' . $vehicleImg . '"/>' . 
        '<h3>' . get_post_meta($currentPostID,'vehicle-year', true) . ' ' . 
        get_post_meta($currentPostID,'vehicle-make', true) . ' ' . 
        get_post_meta($currentPostID,'vehicle-model', true) .'</h3><p>' .
        get_post_meta($currentPostID,'vehicle-mileage', true) . ' ' . 
        get_post_meta($currentPostID,'vehicle-transmission', true) . '<br /><strong>' . 
        get_post_meta($currentPostID,'vehicle-retail-price', true) . '</strong></p></div></div><a href="' . get_permalink($currentPostID, false) . '"></a>';
    }
    return $querySedans;
}
  • You have beaten me to this. I have deleted my answer and added my extra info to your answer. ;-). If anyone has an issue with this, please feel free to rollback my edit ;-) – Pieter Goosen Jul 31 '15 at 5:39
  • Ha, I will be more descriptive next time :) – Marek Jul 31 '15 at 6:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.