Support » Plugins » Hacks » Get 5 recent posts from custom post type in a shortcode

  • I have a wordpress shortcode which displays selected posts.

    <?php
    /**
     * Special Deals
     *
     * To override this shortcode in a child theme, copy this file to your child theme's
     * theme_config/extensions/shortcodes/shortcodes/ folder.
     *
     */
    
    function tfuse_special_deals($atts, $content = null)
    {
        extract( shortcode_atts(array('title' => '','cars' => '','link' => '' ), $atts) );
        $html = '';
        $offers = explode(',',$cars);
        $args = array(
            'posts_per_page'  => -1,
            'orderby'         => 'post_date',
            'order'           => 'DESC',
            'include'         => $offers,
            'post_type'       => TF_SEEK_HELPER::get_post_type(),
            'post_status'     => 'publish',
        );
        $posts_array = get_posts( $args );
    
        $html .= '<div class="special_offers">
            <h2>'.tfuse_qtranslate($title).'</h2>
            <div id="special_offers">';
            $price_symbol = TF_SEEK_HELPER::get_option("seek_property_currency_symbol","$");
            $fuel_symbol = TF_SEEK_HELPER::get_option("seek_property_consumption_symbol","MPG");
            foreach($posts_array as $car){
                $car_link = get_permalink($car);
                $image = new TF_GET_IMAGE();
                $img = $image->width(310)->height(134)->src(tfuse_get_car_thumbnail($car->ID))->get_img();
                $price = TF_SEEK_HELPER::get_post_option('property_price', 0, $car->ID);
                $fuel = apply_filters('tfuse_fuel_number_format', TF_SEEK_HELPER::get_post_option('property_consumption', 0, $car->ID));
                $date = '01/'.TF_SEEK_HELPER::get_post_option('property_year', '11/2010', $car->ID);
                $date = date("d/m/Y", strtotime($date));
                $date = strtoupper(date("M Y",strtotime($date)));
    
                $fuel_out = apply_filters('tfuse_power_fuel_symbol_position', tfuse_symbol_position($fuel, $fuel_symbol, 1, ' ', true), $fuel, $fuel_symbol);
    
                $price_number_str   = apply_filters( 'tfuse_price_number_format', number_format($price,0,'', '.'), $price );
                $price_symbol_str   = '<span class="symbol_price_left">' . $price_symbol .'</span>';
                $price_symbol_pos   = TF_SEEK_HELPER::get_option('seek_property_currency_symbol_pos', 0);
                $out = tfuse_symbol_position($price_number_str, $price_symbol_str, $price_symbol_pos, '', true);
    
                $html.='<div class="special_item">
                    <div class="special_image"><a href="'.$car_link.'">'.$img.'</a></div>
                    <div class="special_text">
                        <h3><a href="'.$car_link.'">'.$car->post_title.'</a></h3>
                        <div class="info_row"><span>'.__('FIRST REG:','tfuse').'</span> '.$date.'</div>
                        <div class="info_row"><span>'.__('FUEL CONS:','tfuse').'</span> '. $fuel_out .'</div>
                        <div class="info_row"><span>'.__('MILEAGE','tfuse').'</span> '.number_format(TF_SEEK_HELPER::get_post_option('property_mileage', 1, $car->ID),0,'',',').'</div>
                        <div class="special_price">' . $out . '</div>
                    </div>
                </div>';
            }
    
            $html .= '</div><a class="prev" id="special_offers_prev" href="#"></a><a class="next" id="special_offers_next" href="#"></a>
            <div class="link_more"><a href="'.$link.'">'.__('View All Special Offers','tfuse').'</a></div>
            <script>
            jQuery(document).ready(function() {
                function carSpecicalInit() {
                    var car_specical = jQuery("#special_offers");
                                car_specical.carouFredSel({
                                    prev : "#special_offers_prev",
                                    next : "#special_offers_next",
                                    infinite: true,
                                    circular: false,
                                    auto: false,
                                    width: "100%",
                                    direction: "down",
                                    scroll: {
                                        items : 1
                                    }
                                });
                            }
                jQuery(window).load(function() {
                    carSpecicalInit();
                });
                var resizeTimer;
                jQuery(window).resize(function() {
                    clearTimeout(resizeTimer);
                    resizeTimer = setTimeout(carSpecicalInit, 100);
                });
            });
            </script>
        </div>';
    
        return $html;
    }
    
    $atts = array(
        'name' => __('Special Deals','tfuse'),
        'desc' => __('Here comes some lorem ipsum description for the box shortcode.','tfuse'),
        'category' => 7,
        'options' => array(
            array(
                'name' => __('Title','tfuse'),
                'desc' => __('Text to display above the box','tfuse'),
                'id' => 'tf_shc_special_deals_title',
                'value' => 'SPECIAL DEALS',
                'type' => 'text'
            ),
            array(
                'name' => __('Specify car','tfuse'),
                'desc' => __('Specify the cars for special deals','tfuse'),
                'id' => 'tf_shc_special_deals_cars',
                'value' => '',
                'type' => 'multi',
                'subtype' => TF_SEEK_HELPER::get_post_type()
            ),
            array(
                'name' => __('Link','tfuse'),
                'desc' => __('Link for all offers','tfuse'),
                'id' => 'tf_shc_special_deals_link',
                'value' => '#',
                'type' => 'text'
            ),
        )
    );
    
    tf_add_shortcode('special_deals', 'tfuse_special_deals', $atts);

    And I want not to choose them manually but to get 5 last added. I found universal code for get recent posts but I don’t know where to put it.

    <ul>
    // Define our WP Query Parameters
    <?php $the_query = new WP_Query( 'posts_per_page=5' ); ?>
    
    // Start our WP Query
    <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
    
    // Display the Post Title with Hyperlink
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    
    // Display the Post Excerpt
    <li><?php the_excerpt(__('(moreā€¦)')); ?></li>
    
    // Repeat the process and reset once it hits the limit
    <?php
    endwhile;
    wp_reset_postdata();
    ?>
    </ul>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Steven Stern

    (@sterndata)

    Support Team / Sword in the Darkness

    Put this in your theme’s functions.php:

    add_shortcode( 'sds_recent_posts', 'sds_recent_posts' );
    function sds_recent_posts( $atts, $content ) {
            $post_type_slug = 'you_need_to_set_this'
    	$args = array(
    		'posts_per_page' => 5,
    		'offset' => 0,
    		'post_type' => $post_type_slug,
    		'orderby' => 'post_date',
    		'order' => 'DESC',
    		'post_type' => 'post',
    		'post_status' => 'publish',
    		'ignore_sticky_posts' => true,
    	);
    	$recent_posts = new WP_Query( $args );
    	ob_start();
    	if ( ! $recent_posts-> have_posts() ) {
    		return 'No Posts Found for ' . $post_type_slug;
    	}
    	while ( $recent_posts->have_posts() ) {
    		$recent_posts->the_post();
    		the_title( '<h2>', '</h2>' );
    		the_excerpt();
    		// you may want to add other stuff here
    	}
    	wp_reset_query();
    	return ob_get_clean();
    }

    You could also build a shortcode around wp_recent_posts: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

    it isn’t work or i can’t combine two codes.
    Is there a way to make the shortcode show 5 recent instead of type manual ID’s?

    Moderator Steven Stern

    (@sterndata)

    Support Team / Sword in the Darkness

    I gave you an alternate shortcode to use that gets 5 recent posts for a CPT. If you want to change the original shortcode, please contact its developer.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get 5 recent posts from custom post type in a shortcode’ is closed to new replies.