Display Random Product Link - wordpress

I need PHP-code for WordPress (Woocommerce) to display random product link.
For example, I have Product1 and want to display on this page (in description of my product):
"See also other products: [Product2 - link] and [Product3 - link]"
Don't know how, I just need php code to insert it in post/pages/products and everywhere I want on my site.
I'm not a coder and I found this code, for example, to display page title with link, but it's not what I need
<?php
echo ''.get_the_title($product_id).'';
?>
But how to get random product, don't know, thanks for help.

Try this :
$args = array(
'posts_per_page' => 1,
'orderby' => 'rand',
'post_type' => 'product' );
$random_products = get_posts( $args );
foreach ( $random_products as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?>
<?php endforeach;
wp_reset_postdata();

The Perfect solution for outputting a single random product which can be achieved using the following code.
<?php
$post_array=array();
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
array_push($post_array,get_the_ID());
endwhile;
$random_key = array_rand($post_array, 1);
echo ''.get_the_title($post_array[$random_key]).'';
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
Have tested the same for you. It worked. Lemme Know if the same worked for you too.

Related

How to show ACF in a WP_Query loop with 'post_type' => 'product'

Try to achieve this:
I made a ACF (custom field) in my WooCommerce Product and now I try to get this field shown in my template with this code:
<ul class="products">
<?php
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_type',
'field' => 'name',
'terms' => 'grouped',
),
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$linked_with_items = the_field('linked_with_items');
the_title('<strong>', '</strong>'); echo '<br />';
echo $linked_with_items;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
But whatever I try also with get_field() the field does not show in my template.
Can someone help?
https://www.advancedcustomfields.com/
This is the final code fyi
<?php if( have_rows('catalogue') ): ?>
<?php
while( have_rows('catalogue') ): the_row(); // catalogue is the field
the_sub_field('linked_with_items'); ?>
<?php endwhile; ?>
<?php endif; ?>
You could try with this:
$linked_with_items = get_field('linked_with_items', get_the_ID());
If that doesn't work, just as a test, you could try to simply loop over posts with a foreach
foreach ( $loop->posts as $post ) {
$linked_with_items = get_field('linked_with_items', $post->ID);
}
If none of those work, please make sure that your product actually have that custom field, double check the ACF field settings (rule section), the field slug, and double check your product edit page to see if the fields shows there.

How to add custom template link to grouped products?

I am working on woocommerce and showing listing of group products for specific category. When i am clicking on any product then it redirect to single product page which is default functionality of woocommerce. Now i want to add custom link to all group products. e.g.
http://test.com/product-title/testproduct?c=12&g=1
These parameters "C" and "G" id are coming dynamically. But when i click on this link then it redirects to product detail page. Is there a way to add custom template link to product. This is my code to get products.
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'washer' );
$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();
Code should be
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'lego-bricks' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$product_link = '';
if($product->is_type('grouped')) {
$product_link = site_url().'/custom-link/?c=12&g=1';
} else {
$product_link = get_permalink();
}
echo '<br /><a href="'.$product_link.'">'.
woocommerce_get_product_thumbnail().' '.get_the_title().'</a>';
endwhile;
wp_reset_query();
Make sure your page with custom link has been assigned custom page template.

How to not repeat wordpress loop/query?

This is a bad habit i've been using in wordpress and i want to stop. I repeat the loop/query code when i should fetch all at once. Here is an example...
<?php
$args = array(
'p' => 9345, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
$args = array(
'p' => 9341, // id of a page, post, or custom type
'post_type' => 'front page content');
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content(); endwhile;
?>
If i want to display the content of the id 9345 first and the content of the id 9341 second, how do i do this all without duplicating WP_Query? I know about order_by but what if i want to specifically order results from the query?
You can use post__in, Read more
<?php
$args = array(
'post__in' => array(9345, 9341),
'post_type' => 'page'
)
$loop = new WP_Query( $args );
//Display the contents
while ( $loop->have_posts() ) : $loop->the_post();
the_content();
endwhile;
//DONT FORGET THIS
wp_reset_postdata();
?>

related products on woocommerce

i have next loop:
<?php woocommerce_product_loop_start(); ?>
<?php
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
From some reason that's display always the same 4 products. what i want is - that on every refresh it will display differents products from all categories i have on WooCommerce.
What i need to add to the loop, or should i need to create something else for that?
it might be a conflict with plugin. Some plugins disable ability for random ordering unless you filter it out with: remove_all_filters('posts_orderby');.
So try to put that before your query:
<?php
remove_all_filters('posts_orderby');
// Setup your custom query
$args = array('post_type' => 'product','posts_per_page' => '4','orderby' => 'rand');
$loop = new WP_Query( $args );

wordpress - ordering multiples post types

i'm trying to order multiples post types in a page
my solution below is working but the posts are not showing by type after type exactly
<?php $args = array_merge( $wp_query->query,
array( 'post_type' =>array( 'editorial','video','portfolio' ),'posts_per_page=-1','orderby'=>'post_type') );
}
query_posts( $args );
$postType='';
if (have_posts()) : while (have_posts()) : the_post();
$PT = get_post_type( $post->ID );
if($postType != $PT){
$postType = $PT;
echo '<p class="clearfix"><h1>All posts of type : '.$postType.'</h1></p>';
}
?>
<?php
if($postType == 'editorial'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'video'){ ?>
<?php echo $postType; ?>
<?php }elseif($postType == 'portfolio'){
<?php echo $postType; ?>
}
?>
and it output like that:
All posts of type : editorial
editorial
editorial
editorial
All posts of type : video
video
video
All posts of type : editorial //problem
editorial
All posts of type : portfolio
portfolio
portfolio
portfolio
portfolio
thank you in advance
According to the Codex, post_type isn't one of the allowed values for the orderby parameter.
One way around your problem would be to use the posts_orderby filter, something like this:
function order_posts_by_type($original_orderby_statement) {
global $wpdb;
return $wpdb->posts .".post_type ASC";
}
add_filter('posts_orderby', 'order_posts_by_type');
$custom_query = new WP_Query( array(
'post_type' =>array( 'editorial','video','portfolio' ),
'posts_per_page' => '-1',
'orderby'=>'post_type',
'order' => 'ASC'
) );
remove_filter('posts_orderby', 'order_posts_by_type');
FWIW, I'd suggest changing 'posts_per_page=-1' to 'posts_per_page' => -1 to be consistent with your other arguments.

Resources