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();
} ?>
Related
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.
Maybe this questions already asked but that not helpful for my question.
Am creating a API for my WordPress project. So i want to send API for to get all products with pagination.
I get products list using this code:
android/all_products.php
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => 10
);
$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();
?>
I got all products, but i want to show products with pagination.
Note: all the API's are written in inside android folder.
Thanks in advance.
below pagination code is to show next products you can also do it to show previous product by applying same method
if(isset( $_GET['page_num'] ) ) {
$page_number = $_GET['page_num'];
} else {
$page_number = 2;
}
$args = array (
'limit' => 3,
'page' => $page_number,
);
$products = wc_get_products( $args );
foreach( $products as $product ) {`enter code here`
//your data here
// e.g $product_name = $product->get_name();
}
Next
If you want to create API's then just use Woocommerce rest API's which are built in function given in wordpress please use below url it will give you list of products with lots of options:
{{your_url}}/wp-json/wc/v2/products
Go to wp-admin and go to Woocommerce -> settings -> API
-> Enable rest API
-> Go to Key/Apps and create Auth Keys both secret and Private (Copy that keys)
Then give this above url to add it for use in application with o_auth of secret key and private key.
Please see this link for all woocommerce API's : https://woocommerce.github.io/woocommerce-rest-api-docs/#retrieve-a-product
Finally i creating a code and send the date using API.
<?php
require_once('../wp-load.php');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$brand_product_list = new WP_Query( $args);
$pagination_count = 1;
while($brand_product_list->have_posts()) : $brand_product_list->the_post();
$pagination_count++;
endwhile; wp_reset_query();
//echo'<pre>';print_r($pagination_count/12); exit;
wp_reset_query();
?>
<?php
$pagination = round($pagination_count/12);
for($i=1;$i<=$pagination;$i++)
{
$pagination_no[] = $i;
?>
<!-- <a class="product-category-view-all" href="?pagination=<?php echo $i; ?>"><?php echo $i; ?></a> -->
<?php } ?>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : $_GET['pagination'];
$args = array(
'post_type' => 'product',
'paged' => $paged,
'posts_per_page' => 12,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
$product_data = wc_get_product( $post->ID );
if(!empty(get_the_post_thumbnail_url($product_data->get_id())))
{
$img = get_the_post_thumbnail_url($product_data->get_id());
}
else
{
$img = "";
}
$product_list[] = array(
'product_id' => $product_data->get_id(),
'product_name' => $product_data->get_title(),
'product_regular_price' => $product_data->get_regular_price(),
'product_sale_price' => $product_data->get_sale_price(),
'product_price' => $product_data->get_price(),
'img' => $img,
'rating' => $product_data->get_average_rating(),
'stock_quantity' => $product_data->get_stock_quantity(),
'stock' => $product_data->is_in_stock(),
);
endwhile; wp_reset_query();
$data[] = array(
'pagination' => $pagination_no,
'product_list' => $product_list
);
//echo json_encode($peoduct_list, $pagination)
echo json_encode($data)
?>
Building a Wordpress page template for a custom taxonomy and related custom post type. Within a new WP_Query I need to grab fields from (2) different ACF post object fields; list-staff and list-rep. Code works as expected up to the wp_reset_postdata(); correct amount of results are returned, data within each post is unique up to the point of the reset. After the reset, all data is the same within each post. Code follows, and I'm certain there's a more elegant solution:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php the_field('list_number'); ?>
<?php
$staff = get_field('list_staff');
$rep = get_field('list_rep');
if( $staff ):
// override $post
global $post;
$post = $staff;
setup_postdata( $post );
?>
<?php the_permalink(); ?><?php the_title(); ?>
<?php
endif;
if( $rep ):
// override $post
$post = $rep;
setup_postdata( $rep );
?>
<?php the_field('mkt_co'); ?><?php the_field('mkt_tel'); ?>
<?php
endif;
wp_reset_postdata();
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
Figured this one out. setup_postdata() is a completely wrong direction for this application. What is correct is documented on the ACF page for "displaying data for multiple post objects." As the article states "Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question." Read more about it here; https://www.advancedcustomfields.com/resources/post-object/. My working code follows:
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'parade-of-homes',
'parade-category' => 'parade-homes',
'posts_per_page' => -1,
'meta_key' => 'entry_number',
'orderby' => 'meta_value',
'order' => 'ASC'
);
$listing = new WP_Query( $args );
if ( $listing->have_posts() ) :
while ( $listing->have_posts() ) : $listing->the_post();
?>
<?php
the_field('list_number');
$post_object = get_field('list_staff');
if( $post_object ):
?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<?php echo get_the_title($post_object->ID); ?>
<?php
endif;
?>
<?php
$post_object = get_field('list_rep');
if( $post_object ):
?>
<p><?php the_field('mkt_co', $post_object->ID); ?></span></p>
<?php the_field('mkt_tel', $post_object->ID); ?>
<?php
endif;
?>
<?php the_field('list_address') ?>
<?php
endwhile;
endif;
wp_reset_query();
?>
Added this code to display wp custom post by category, but unable to get pagination to work when added posts_per_page="5"
<?php query_posts('post_type=encounters_news'); while (have_posts()) : the_post(); ?>
<?php $terms = get_the_terms($post->ID, 'encounters_news_categories');
foreach($terms as $item):
if($item->slug == "chapter-news"):?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php //comments_template( '', true ); ?>
<?php endif; ?>
<?php endforeach; ?>
<?php endwhile; wp_reset_query(); ?>
<?php encounters_content_nav( 'post-nav' ); ?>
Rewrote and change to this code
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$post_type = 'encounters_news';
$tax = 'chapter-news';
$tax_terms = get_terms($tax);
if ($tax_terms) {
foreach ($tax_terms as $tax_term) {
$query_args = array(
'post_type' => $post_type,
'$tax' => $tax_term->slug,
'showposts' => 5,
'paged' => $paged
); wp_reset_query();
}}
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args ); ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php get_template_part( 'content-news', 'page' ); ?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="navigation">
<div class="alignleft"><?php echo get_previous_posts_link( '« Previous' ); // display newer posts link ?></div>
<div class="alignright"><?php echo get_next_posts_link( 'More »', $the_query->max_num_pages ); // display older posts link ?></div>
This code creates the pagination as desired, but displays all the Encounters News Categories instead of just the Chapter News category.
Suggestions will be greatly appreciated.
Resolved
<?php
$query = new WP_Query( array(
'post_type' => '', // name of post type.
'tax_query' => array(
array(
'taxonomy' => '', // taxonomy name
'field' => '', // term_id, slug or name
'terms' => , // term id, term slug or term name
)
),'showposts' => 5,
'paged'=>$paged
) );
while ( $query->have_posts() ) : $query->the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();?>
I need your help..
i've two categories in wordpress to display post
1. News
2. Projects
now i want to display last 3 post from both category like below..
post1.
post2.
post3.
post1.
post2.
post3.
Thank you guys..
you can try something like below code it will fetch 3 post from each category
<?php
wp_reset_query();
$cats = get_categories('');
foreach ($cats as $cat) :
if($cat->category_parent) continue;
$args = array(
'posts_per_page' => 3,
'category_name' => $cat->slug,);
query_posts($args);
if (have_posts()) :
echo '<h2>Latest Posts in '.$cat->name.' Category</h2>';
?>
<?php while (have_posts()) : the_post(); ?>
<div>
<h2>
<?php the_title(); ?>
</h2>
</div>
<?php
if ( is_category($vidcat) ) { the_content(); }
else { echo strip_tags(get_the_excerpt(), '<a><strong>'); }
?>
<!-- this area is for the display of your posts the way you want it -->
<!-- i.e. title, exerpt(), etc. -->
<?php endwhile; ?>
<?php else : echo '<h2>No Posts for '.$cat->name.' Category</h2>';?>
<?php endif; wp_reset_query; ?>
<?php endforeach; ?>
if you want to fetch exclude any category than pass the argument like this.
$args = array(
'exclude' => '' //pass category id which your don't want to include.
)
$cats = get_categories($args);
Thank you guys for helping me...
I found solution which is working..
Thanks a lot for support..
$args1 = array( 'posts_per_page' => 3, 'offset'=> 0, 'category' => 3,'post_type'=> 'post','post_status'=>'publish','orderby'=> 'post_date','order'=> 'DESC','suppress_filters' => true);?>
<?php
$categoryids = array(add news category id,add projects category id);
$args = array(
'numberposts' => 3,
'category__in' => $categoryids,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'give the name of post types',
'post_status' => 'publish' );
$posts_array = get_posts( $args );
foreach ($posts_array as $posts) {
....................
}
?>