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

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.

Related

Remove duplicate post variation in WooCommerce

Need a query to remove duplicate post variation like this situation, in all products:
Just load the product edit page by adding this code into the functions.php file of your active theme, then remove the code. Please change the product ID in the code below.
<?php if (isset( $_GET['post'])) {
$current_list = array();
$args = array(
'posts_per_page' => 500,
'post_type' => 'product_variation',
'orderby' => 'title',
'order' => 'asc',
'offset' => 0,
'ID' => 1631819251, //Parent ID
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts()) {
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?><br>
<?php $this_variation_title = get_the_title();
if (in_array($this_variation_title, $current_list)) {
echo $this_variation_title.' duplicate deleted.<br>';
wp_delete_post(get_the_ID());
} else {
array_push($current_list, $this_variation_title);
echo 'Variation added to array.<br>';
} ?>
<?php endwhile; ?>
<?php } ?>
<?php wp_reset_postdata();
} ?>

How to get the post of selected subcategory only

I have three drop-down select option. Inside each drop-down, I have shown the Sub-category for example shown below.
Service
Mobile development
Web development
Software development
Technology
Android
IOS
Java
Industry
Commerce
Education
Lifestyle
Here Service, Technology, and Industry are the category of CPT and inside it like Mobile Development, IOS and Commerce are the sub-categories of the main category.
Now what I want to do on change of any of the three category show post of that sub-category only.
For example, if I select IOS then show the post of IOS only similar with others if the user selects IOS, Mobile Development, and Lifestyle then show post of all three.
To achieve this I have to use the change jQuery function and calling the ajax on change event. The problem is I am getting the same post for all the post select please help me through it.
You Can achieve this by Following this -
Firstly You Register a Custom Post Type like 'Course' in my Code.
Then you need to Add Custom Taxonomy For this like 'Course Type' in my Code.
Then get All Posts with using Page Template or Any other method which you are followed.
I add this code in my Page Template then attach those Page Template to a Page, Here i get all posts when page reload with thre Select boxes for Parent Terms like 'Service', 'Technology', 'Industry' -
<div class="course_content">
<?php $parent_terms = get_terms( 'course_type', array( 'parent' => 0 ) );
foreach( $parent_terms as $parent_term ){
$children_terms = get_terms('course_type', array('parent' => $parent_term->term_id) );
if($children_terms){
?>
<select class="select_course_type <?php echo 'course_type-'.$parent_term->slug; ?>">
<option value=""><?php echo 'Select '.$parent_term->name; ?></option>
<?php
foreach( $children_terms as $children_term ){
echo '<option value="'.$children_term->slug.'">'.$children_term->name.'</option>';
}
?>
</select>
<?php
}
}
?>
<input type="hidden" value="" id="selected_course_type" />
<div class="display_posts">
<?php
$args = array(
'post_type' => 'course',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) :
echo '<ul>';
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<h4><?php the_title() ;?></h4>
</li>
<?php endwhile;
echo '</ul>';
endif;
wp_reset_query(); ?>
</div>
</div>
Then you add jquery code in your JS File For Calling Ajax, like this -
$('.select_course_type').on('change', function(){
var s = $(this).val();
$("option", this).removeAttr('selected');
$("option[value='" + s + "']", this).attr('selected', 'selected');
ct_Array = [];
$('.select_course_type').each(function(){
var t = $(this).val();
if(t != ''){
ct_Array.push($(this).val());
}
});
var ajax_url = FrontAjax.ajaxurl;
data = {
action: 'ajax_filter_for_courses',
s_ctValues: ct_Array,
};
$.post( ajax_url, data, function(response) {
if( response ){
$('.display_posts').html(response);
}
});
});
Then add this code in Functions.php File for getting Posts after Filter-
add_action('wp_ajax_nopriv_ajax_filter_for_courses', 'filter_for_courses');
add_action('wp_ajax_ajax_filter_for_courses', 'filter_for_courses');
function filter_for_courses() {
$explode_ct = $_POST['s_ctValues'];
$tax_query = array();
if(isset($explode_ct)) {
$tax_query[] = array(
'taxonomy' => 'course_type',
'field' => 'slug',
'terms' => $explode_ct
);
}
$args = array(
'post_type' => 'course',
'tax_query' => $tax_query
);
$filter_query = new WP_Query( $args );
if ( $filter_query->have_posts() ) {
echo '<ul>';
while ( $filter_query->have_posts() ) : $filter_query->the_post();
?>
<li>
<h4><?php the_title() ;?></h4>
</li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
die(); //stop "0" from being output
}
$posts_array = get_posts(
array(
'posts_per_page' => 6,
'post_type' => 'acme_portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_category',
'field' => 'term_id',
'terms' => array($post_id),
)
)
));
Put this in function.php

Pagination fails with CPT and dynamic tax_query

I have a search form. Choose a taxonomy, and select one or more terms. I want to paginate the results.
Here is the code:
<?php
if(isset($_POST['tax_type'])) {
$tax_type=$_POST['tax_type'];
$terms = $_POST[$tax_type];
$term_list = implode("','", $terms);
$term_list1 = implode(", ", $terms);
$term_list = "'".$term_list."'";
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$giftfinder_args = array(
'post_type' => 'products',
'posts_per_page'=>"1",
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $tax_type,
'field' => 'slug',
'terms' => explode(',',$term_list),
),
),
);
echo '<div class="results"><span class="eyebrow">search results</span>';
echo '<div class="cat_label">“' . $term_list1 . '”</div></div>';
// the query
$giftfinder_query = new WP_Query( $giftfinder_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $giftfinder_query;
?>
<?php if ( $giftfinder_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $giftfinder_query->have_posts() ) : $giftfinder_query->the_post();
$thumb = get_the_post_thumbnail(null, 'medium');
?>
<div class="prod"><?php if(empty($thumb)) {echo '<div style="width:265px;height:265px;background:#eee;"></div>';} else {the_post_thumbnail('medium');} ?><span class="truncate"><?php the_title(); ?></span></div>
<?php endwhile; ?>
<div style="clear:both;"></div>
<!-- end of the loop -->
<?php
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
?>
<?php wp_reset_postdata(); ?>
<?php else : get_template_part( 'content', 'none' ); endif; ?>
<?php } ?>
First page is fine, but no results appear on paginated pages.
As you can see, I'm trying to use the pagination fix that turns my query into $wp_query:
// the query
$the_query = new WP_Query( $args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
But like I said, no results beyond first page.
I was going to try pre_get_posts but I can't figure out how to code it since every query has unique arguments based on the user's choice of taxonomy and searched terms.
I also tried 'add_args' to the paginate_links() function but could not figure out how to format the resulting get string.
BTW, right now, the query just returns one post to make it easy to check pagination.
Any suggestions appreciated!
Not sure if this is the only issue, but it looks like you have a typo.
You are setting $page:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
But looking for $paged:
'paged' => $paged,

Display Random Product Link

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.

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