Query in shortcode
How to Query in Shortcode WordPress
Most of the time may need to query in shortcode to populate WordPress post. In this tune I am going to describe how to query shortcode in WordPress. In a previous tune I described introducing of shortcode but the shortcode little complex than previous ones. Let’s go on the below code where describe every thing as comment.
function registering_qury_shortcode($atts){ extract( shortcode_atts( array( 'category' => '', ), $atts, 'shortcode_name' ) ); $q = new WP_Query( array('posts_per_page' => '-1', 'post_type' => 'post_type_here', 'category_name' => $category) ); $list = ''; //In the quotation will be before div or wrapper queriable post while($q->have_posts()) : $q->the_post(); $idd = get_the_ID(); <!-- $any_veriable_name = get_post_meta($idd, 'custom_feild_ID_name', true); $portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-thumb' ); --> $list .= ''; //In the quotation will be single queriable post <!-- here can be use:: '.get_the_content().' = for get post content '.get_the_title().' = for get post title '.get_permalink().' = for get post permalink '.get_the_time('M d, Y').' = for get post date '.get_the_post_thumbnail().' = for get post thumbnail '.$portfolio_thumb[0].' = for get custom field image '.$any_veriable_name.' = for get custom field data --> endwhile; $list.= ''; //In the quotation will be after div or wrapper queriable post wp_reset_query(); return $list; } add_shortcode('shortcode_name', 'registering_qury_shortcode');
In array(‘posts_per_page’ => ‘-1’, ‘post_type’ => ‘post_type_here’, ‘category_name’ => $category) use you post-type to get post.
Here can use, '.get_the_content().' = for get post content '.get_the_title().' = for get post title '.get_permalink().' = for get post permalink '.get_the_time('M d, Y').' = for get post date '.get_the_post_thumbnail().' = for get post thumbnail Taking the variable, $any_veriable_name = get_post_meta($idd, 'custom_feild_ID_name', true); $portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-thumb' ); The variable use as '.$portfolio_thumb[0].' = for get custom field image '.$any_veriable_name.' = for get custom field data I would like show a example query shortcode below:
function post_carousel_shortcode($atts){ extract( shortcode_atts( array( 'type' => 'post', ), $atts, 'team' ) ); $q = new WP_Query( array('posts_per_page' => -1, 'post_type' => $type) ); $list = '<div id="owl-portfolio" class="owl-carousel carousel-th">'; while($q->have_posts()) : $q->the_post(); $idd = get_the_ID(); $team_sub = get_post_meta( $idd, 'team_subtitle', true ); $portfolio_large = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-large' ); $portfolio_thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'portfolio-thumb' ); $list .= ' <div class="item"> <figure class="icon-overlay icn-enlarge"><a href="'.$portfolio_large[0].'" class="fancybox-media" data-rel="portfolio"> <img src="'.$portfolio_thumb[0].'" alt="" /> </a></figure> <div class="bordered no-top-border"> <div class="info"> <h3 class="post-title"><a href="'.get_permalink().'">'.get_the_title().'</a></h3> <div class="meta"> <span class="date">'.get_the_time('M d, Y').'</span></div> </div> </div> </div> '; endwhile; $list.= '</div>'; wp_reset_query(); return $list; } add_shortcode('carousel', 'post_carousel_shortcode');
If you can not understand to use query in shortcode wordpress comment here.
Comments are closed