In the wordpress woocommerce web site’s contains various featured products and sometimes we want to get the featured products without any plugins.For a example we want to get featured products to create featured slider.So I’m going to tell you have to get woocommerce featured products without any plugins and grammatically.
You can get featured products with using simple worpdress query. But you must understand the correct featured product meta key is the _featured NOT the featured.
Method -1
$args = array( 'post_type' => 'product', 'post_status' => 'publish', 'meta_key' => '_featured', 'meta_value' => 'yes', 'posts_per_page' => 5 ); $featured_query = new WP_Query( $args ); if ($featured_query->have_posts()) { while ($featured_query->have_posts()) { $featured_query->the_post(); $product = get_product( $featured_query->post->ID ); // product contents goes here } } wp_reset_query();
Method -2
query_posts(array( 'post_type' => 'product', 'post_status' => 'publish', 'meta_query' => array( array( 'key' => '_visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN' ), array( 'key' => '_featured', 'value' => 'yes' ) ) ) ); if (have_posts()) { while (have_posts()) { the_title(); the_post(); // ADDITIONAL $img_id = get_post_thumbnail_id($post->ID, 'thumbnail'); // This gets just the ID of the img $image = wp_get_attachment_image_src($img_id); $alt_text = get_post_meta($img_id , '_wp_attachment_image_alt', true); $sku = get_post_meta($post->ID , '_sku'); $price = get_post_meta($post->ID , '_price'); } } wp_reset_query();
That’s only.