Show questions by shortcode

4.10K viewsUpdates
0

My Site template name is “Espresso”. link
This template has a Shortcodes to display posts from a category. link & link
I’m going with this shortcode can also display the latest questions and answers my questions plugin(anspress) categories.

One of Shortcodes are used to display the latest posts from one category and only support the original categories of WordPress. I’m gonna do I use this shortcode to display a custom post type. I installed Anspress. it creates a custom post type as “questions”, and I want to show my last questions with that shortcode.

my theme shortcode function:

if ( ! function_exists( 'vw_shortcode_posts' ) ) {
	function vw_shortcode_posts( $atts, $content = null ) {
		global $vw_posts_shortcode_id;
		
		$defaults = array(
			'title' => '',// title
			'cat' => '',// category ID
			'cat_name' => '',// category name
			'cat_exclude' => '', // category IDs, separated by comma (,)
			'tag' => '', // tag slugs, separated by comma (,)
			'layout' => 'medium-1-col-3',
			'count' => '6',
			'offset' => '0',
			'order' => 'latest', // latest, random, popular, viewed, latest_reviews
			'pagination' => 'hide',
		);
		
		extract( shortcode_atts( $defaults, $atts) );

		global $post;

		$query_args = array(
			'post_type' => 'post',
			'ignore_sticky_posts' => true,
			'posts_per_page' => $count,
			'paged' => vw_get_paged(),
			'order' => 'DESC',
			// 'meta_key' => '_thumbnail_id', // DEV: Only posts that have featured image
		);

		// Option: offset
		if ( intval( $offset ) > 0 ) {
			$query_args['offset'] = intval( $offset );

			if ( vw_get_paged() > 1 ) {
				// WordPress is not support Offset on Pagination. This is a hack.
				$query_args['offset'] += ( vw_get_paged() - 1 ) * $count;
			}
		}

		// Option: cat_name
		if ( ! empty( $cat_name ) ) {
			$query_args['category_name'] = $cat_name;

			if ( ! empty( $title ) ) {
				$category = get_category_by_slug( $cat_name );
				if ( ! empty( $category ) ) {
					$title = '<span class="'.esc_attr( vw_get_the_category_class( $category->term_id ) ).'">'.$title.'</span>';
				}
			}
		}

		// Option: cat
		if ( ! empty( $cat ) ) {
			$query_args['taxonomy'] = $cat;

			if ( ! empty( $title ) ) {
				$title = '<span class="'.esc_attr( vw_get_the_category_class( $cat ) ).'">'.$title.'</span>';
			}
		}

		// Option: cat_exclude
		if ( ! empty( $cat_exclude ) ) {
			$query_args['category__not_in'] = explode( ',', $cat_exclude );
		}

		// Option: tag
		if ( ! empty( $tag ) ) {
			$query_args['tag'] = $tag;
		}

		// Option: order
		if ( 'random' == $order ) {
			$query_args['orderby'] = 'rand';
			
		} elseif ( 'featured' == $order ) {
			$query_args['meta_query'][] = array(
				'key' => 'vw_post_featured',
				'value' => '1',
				'compare' => '=',
			);
			
		} elseif ( 'latest_gallery' == $order ) {
			$query_args['tax_query'][] = array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-gallery',
			);

		} elseif ( 'latest_video' == $order ) {
			$query_args['tax_query'][] = array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-video',
			);

		} elseif ( 'latest_audio' == $order ) {
			$query_args['tax_query'][] = array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-audio',
			);

		} elseif ( 'latest_reviews' == $order ) {
			$query_args['meta_query'][] = array(
				'key' => 'vw_enable_review',
				'value' => '1',
				'compare' => '=',
			);

		} elseif ( 'most_viewed' == $order ) {
			$query_args['orderby'] = 'meta_value_num';
			$query_args['meta_key'] = 'vw_post_views_all';
			
		} elseif ( 'most_review_score' == $order ) {
			$query_args['orderby'] = 'meta_value_num';
			$query_args['meta_key'] = 'vw_review_average_score';

		} else { // 'latest' == $order
			$query_args['orderby'] = 'post_date';
		}

		query_posts( $query_args );

		$template_file = sprintf( 'templates/post-loop/loop-%s.php', $layout );

		ob_start();
		?>
		<?php

		wp_reset_query();

		return ob_get_clean();
	}
}

tanks

Your question is not clear to me. Please re-edit and let us know what you trying to do? Cheers!

@Rahul Aryan
Apologize for my poor English language.