Show categories not in gallery post format - wordpress

I want to show all blog categories that aren't of type gallery. My code almost works but if I have 2 posts in the same category the category is shown twice ie:
If I create a category called 'news' and add 2 non-gallery posts, it shows up as:
news
news
instead of just
news
<?php
$galleryPosts = new WP_Query(array(
'post_type' => 'post',
'order' => 'ASC'
));
?>
<?php if ( $galleryPosts->have_posts() ) : ?>
<?php while ( $galleryPosts->have_posts() ) : $galleryPosts->the_post(); ?>
<?php if(!has_post_format('gallery')) {
the_category();
}
?>
<?php endwhile; ?>
<?php endif; ?>

To suppress non-gallery posts, try selecting only those posts initially and then remove the has_post_format('gallery') check:
$posts = new WP_Query(
array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-gallery'
),
'operator' => 'NOT IN'
)
)
)
);
Then in PHP:
if ($posts->have_posts()) {
while ($posts->have_posts()) {
$posts->the_post();
the_category();
}
}

Related

woocommerce product shortcodes template

As I know
[products limit=”4″ columns=”4″ on_sale=”true” ] will show all on sale products,
[products limit=”3″ columns=”3″ best_selling=”true” ] will show best selling products,
If I were not wrong, both of the shortcodes would call the content-product.php file to display the products.
My question is how can I make different product shortcodes call different php template files to show the related products instead of calling the same file that is content-product.php?
For example,
on sale shortcodes call content-product-1.php
best selling shortcodes call content-product-2.php
Thank you!
Create our own shortcode to display the specific products and our our own template as below,
for example, wc_get_template_part( 'content', 'product-test' );
add_shortcode( 'sale_products_test', 'sale_products' );
function sale_products( $atts ){
global $woocommerce_loop, $woocommerce;
extract( shortcode_atts( array(
'per_page' => '12',
'columns' => '3',
'orderby' => 'title',
'category' => 'clearance',
'order' => 'asc'
), $atts ) );
// Get products on sale
$product_ids_on_sale = woocommerce_get_product_ids_on_sale();
$meta_query = array();
$meta_query[] = $woocommerce->query->visibility_meta_query();
$meta_query[] = $woocommerce->query->stock_status_meta_query();
$args = array(
'posts_per_page'=> $per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'date',
'order' => 'ASC',
'meta_query' => $meta_query,
'post__in' => $product_ids_on_sale,
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $category,
)),
);
ob_start();
$products = new WP_Query( $args );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product-test' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return ob_get_clean();
}
Not Possible. This type of shortcode require to use wp_query and fetch data your requirement wise

How to show custom texonomy's category related post on single post page

I am working with a custom post type name= product and I have a custom taxonomy name= product-category
on the taxonomy-product.php I am showing a single product and related product based on its single product taxonomy term. But something is missing I can't figure it out.
here is my code.
<?php
//related products
$terms = get_the_terms( $post->ID , 'product-category', 'string');
$term_ids = wp_list_pluck($terms,'term_id');
$second_query = new WP_Query( array(
'post_type' => 'products',
'tax_query' => array(
array(
'taxonomy' => 'product-category',
'field' => 'id',
'terms' => $term_ids,
'operator'=> 'AND' //Or 'AND' or 'NOT IN'
)),
'posts_per_page' => 3,
'ignore_sticky_posts' => 1,
'orderby' => 'rand',
'post__not_in'=>array($post->ID)
) );
if($second_query->have_posts()) {
while ($second_query->have_posts() ) : $second_query->the_post(); ?>
<div class="single_related">
<?php if (has_post_thumbnail()) { ?>
<?php the_post_thumbnail( 'related_sm', array('alt' => get_the_title()) ); ?>
<?php } else { ?>
<?php the_title(); ?>
<?php } ?>
</div>
<?php endwhile; wp_reset_query();
}?>
I have also tried
https://www.banna360.com/display-related-post-product-based-taxonomy-wordpress/
https://code.tutsplus.com/tutorials/show-wordpress-related-posts-with-taxonomy-and-a-custom-post-type--cms-32303

Wordpress display posts from multiple value in meta

I have got main product with meta key: addons and meta values in this key: 129456,968945,495435 Each of these three numbers is the key with these meta values. For example:
Post 1: meta_key: subproduct meta_value: 129456
Post 2: meta_key: subproduct meta_value: 968945
Post 3: meta_key: subproduct meta_value: 495435
And now I want to display these three posts in the main product. My code:
<?php if (!empty($addons = get_post_meta(get_the_ID(), 'addons', true))):?>
<?php
$params = array(
'post_type' => 'product',
'meta_key' => 'subproduct',
'meta_value' => $addons
);
$wc_query = new WP_Query($params);
?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php include(rh_locate_template('inc/parts/woomain.php')); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;?>
With one meta value it worked but with several it doesn't work anymore. How do you view these three posts?
Try to alter your query like this and lets see if that works
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => array($addons),
'compare' => 'IN'
)
)
);
It works with this code:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => $addons,
'compare' => 'IN'
)
)
);

get custom woocommerce product

i want to get 10 product from a category in woocommerce
for example, for get latest post of a posts category i use the following code
<?php $posts = get_posts( 'category=17&numberposts=5' ); ?>
<?php if( $posts ) : ?>
<ul>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li><i class="circle"></i><?php the_title(); ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
i want a code, like this for get woocommerce products
try this example :
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id', //This is optional, as it defaults to 'term_id'
'terms' => 26,
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
$products = new WP_Query($args);
/* your loop */
Hope this will helps you.
Try this example,
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'product_cat' => 'hoodies'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<br />' . woocommerce_get_product_thumbnail().' '.get_the_title().'';
endwhile;
wp_reset_query();
?>
OR
<?php
$args = array( 'post_type' => 'product', 'category' => 34, 'posts_per_page' => -1 );
$products = get_posts( $args );
?>
Hope this will helps you.
For more details please visit,
Woocommerce get products
why not use woocommerce's product_category shortcode?
<?php echo do_shortcode("[product_category category='17' limit='5']"); ?>
It will list the products same as in shop page. With these you are safe with product attributes like if when it's out of stock.

How to display all posts in WordPress?

I want to display all post in my WordPress home page.
I have written following query for getting all post but I do not get all posts. It just displays 10 or 11 posts:
$args = array(
'post_type' => 'post',
'posts_per_page' => $number,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
$args['tax_query'] = array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-video',
));
$query = new WP_Query($args);
So please let me know how can I get all posts.
Displaying all posts that has been published. You have to use post_per_page='-1' to retrive all the posts.
$args = array(
'post_type'=> 'post',
'orderby' => 'ID',
'post_status' => 'publish',
'order' => 'DESC',
'posts_per_page' => -1 // this will retrive all the post that is published
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : ?>
<?php while ( $result->have_posts() ) : $result->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
Hope so this will retrive all the posts as per your expectation.
try Multiple loops :
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<!-- do stuff ... -->
<?php endwhile; ?>
<?php endif; ?>
if you need Advanceded query use this :
<?php query_posts( 'post_type=post&posts_per_page=10'); ?>
and you can use query_posts() get all posts
Set the variable posts_per_page => -1
As so:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'order' => $sort_by,
'orderby' => 'title',
'post_status' => 'publish',
'tag' => $tags,
'ignore_sticky_posts' => 1,
);
This will make Query get all posts in your table.
See more in the WP Query docs

Resources