Automatic Update all posts at once - wordpress

I made some changes in a plugin that creates meta boxes, i added tome default code into the "value" of the meta box.
The data is being sent outside. my problem is that now i need to press "update" on every woocommerce product for it to be permanently saved and sent. But i have 1500 products.
I have tried bulk updating from the general admin area, like Shawn here:
https://bobwp.com/bulk-edit-posts-wordpress/
It didn't work. so i tried this code:
$args = array(
'posts_per_page' => 1000,
'post_type' => 'product'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$casa_id = $post->ID;
update_post_meta($post->ID, 'casa_id', $casa_id);
}
}
And also this:
function woo_zap_update_product_name() {
if ( ! is_admin() ) {
return;
}
$zap_query = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => -1 ) );
if ( $zap_query->have_posts() ) {
global $post;
while ( $zap_query->have_posts() ) {
$zap_query->the_post();
update_post_meta( $post->ID, '_wc_zap_product_name', $post->post_title );
}
wp_reset_postdata();
}
}
add_action( 'init', 'woo_zap_update_product_name' );
Didn't work as well.. any other ideas ?

Related

Products dropdown from same category inside Woocommerce product short description

In Woocommerce, I would like to add a drop down in product short description that shows all products that have the same category(ies). It will be even better if it was possible to go to the product page of the selected product.
I didn't see any threads that fulfill what I am trying to do.
Any help will be appreciated.
2021 Update - Added product_id as argument, allowing the shortcode to be used for a defined product ID (for example on a page).
The following will make a custom shortcode that you can use in your product short description (or even in the product description) and will display a dropdown from same product category than the current product.
The code:
add_shortcode( 'products_dropdown', 'wc_products_from_cat_dropdown' );
function wc_products_from_cat_dropdown( $atts ) {
// Shortcode Attributes
$atts = shortcode_atts( array(
'product_id' => '',
), $atts, 'products_dropdown' );
$product_id = is_product() ? get_the_id() : $atts['product_id'];
if ( empty($product_id) )
return;
ob_start();
$query = new WP_Query( array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '-1',
'post__not_in' => array( $product_id ),
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) ) ,
) ),
) );
if ( $query->have_posts() ) :
echo '<div class="products-dropdown"><select name="products-select" id="products-select">
<option value="">'.__('Choose a related product').'</option>';
while ( $query->have_posts() ) : $query->the_post();
echo '<option value="'.get_permalink().'">'.get_the_title().'</option>';
endwhile;
echo '</select> <button type="button" style="padding:2px 10px; margin-left:10px;">'._("Go").'</button></div>';
wp_reset_postdata();
endif;
?>
<script type='text/javascript'>
jQuery(function($){
var a = '.products-dropdown', b = a+' button', c = a+' select', s = '';
$(c).change(function(){
s = $(this).val();
console.log(s); // just for testing (to be removed)
});
$(b).click(function(){
if( s != '' ) location.href = s;
});
});
</script>
<?php
return ob_get_clean();
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE
1). For single product pages: Just paste the following shortcode in the product short description (or descrition):
[products_dropdown]
2). For single product pages, inside php code:
echo do_shortcode("[products_dropdown]");
3). on any post or page within the text editor, define the product_id argument (below the defined product id is 37):
[products_dropdown product_id="37"]
Add this to your theme's 'functions.php' which will display all products of your current product's category.
function add_products_short_description() {
$product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
if ( $product_cats ) {
$single_cat = array_shift( $product_cats );
$product_args = array( 'post_type' => 'product', 'posts_per_page' => '-1', 'product_cat' => $single_cat->name );
$products = new WP_Query( $product_args );
if ( $products->have_posts() ) : echo '<ul>';
while ( $products->have_posts() ) : $products->the_post(); global $product;
echo '<li>'.get_the_title($products->ID).'</li>';
endwhile;
echo '</ul>';
endif;
}
}
add_action( 'woocommerce_single_product_summary', 'add_products_short_description', 15 );

How to insert new hidden postmeta on a published post ? WordPress

there are a hundred posts to update the postmeta if I do manually. I try to find a way to insert new postmeta based on the post_type and category. I try this:
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
while ( $query->have_posts() ) {
add_post_meta($query->post->ID, '_meta_key', meta_value, true);
add_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
add_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
add_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
add_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}
and it is not working. is there anyway to achieve ?
OK I found the answer here Trigger action when new post is insert in wordpress
Add meta value when post or page or custom post published.
function save_post_meta( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "your post name" != $post_type ){ return;}
update_post_meta( $post_id, 'meta_key', 'value');
}
add_action( 'save_post', 'save_post_meta', 10, 3 );
Use update_post_meta function to update post meta values.
$args = [
'post_type' => 'post',
'category_name' => 'category'
];
$query = new WP_Query($args);
if( $query->have_posts() ) {
$query->the_post();
while ( $query->have_posts() ) {
update_post_meta($query->post->ID, '_meta_key', meta_value, true);
update_post_meta($query->post->ID, '_meta_key2', meta_value2, true);
update_post_meta($query->post->ID, '_meta_key3', meta_value3, true);
update_post_meta($query->post->ID, '_meta_key4', meta_value4, true);
update_post_meta($query->post->ID, '_meta_key5', meta_value5, true);
}
}

Woocommerce update sku of all products? filter?

I'd like to set the SKU to the ID of the product.. but cannot find the right filter to use?
Is there a filter that sends the product SKU?
add_filter( 'woocommerce_get_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
//set $newsku as product id
return $newsku;
}
How do i accomplish this? I have 1000s of products without a SKU. I've tried a plugin called SKU generator but that doesn't work. Any help appreciated.
You can do it by adding following code to your functions.php file and after adding go to you site example.com for only once.
After that remove below code from the functions.php.
add_filter( 'init', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
$random_sku = mt_rand(100000, 999999);
update_post_meta($loop->post->ID,'_sku',$random_sku);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
NOTE : If you don't remove after one use, products SKU will change everytime you visit any page of your site.
Add it as shortcode and make SKU same as Product id.
add_shortcode( 'update_sku', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 12
);
$i=0;
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
update_post_meta($loop->post->ID,'_sku',$loop->post->ID);
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
Then use this shortcode in a page and open it once. so it will not re-create sku's every time.

ordering wordpress post_meta - lowest to highest

I am currently looping through all posts and displaying a post_meta value like this:
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
foreach ( $getLowestPrice as $post ){
get_post_meta( $post->post_id, '_wholesale_price', false );
}
Is there a way to order the results, lowest -> highest? At the moment they are getting displayed randomly, or as they were entered.
use the following code
<?php
global $wpdb;
$table = $wpdb->prefix . 'postmeta';
$theid = get_the_id();
$getLowestPrice = $wpdb->get_results("SELECT * FROM $table WHERE meta_value = '$theid'");
$all_post = array();
foreach ( $getLowestPrice as $post ){
$all_post[] = $post->post_id;
}
$query = new WP_Query( array( 'post__in' => $all_post, 'orderby' => 'meta_value', 'meta_key' => '_wholesale_price','order' => 'ASC') );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<div>' . get_post_meta( $get_the_ID(), '_wholesale_price', false );() . '</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
?>`
This looks a bit wrong to me. If you're trying to order posts by the value of a meta key (unless I'm mistaken, that is what you're doing) then I would normally go about it using WP_Query()
global $wp_query;
$args = array(
'post_type' => '[YOUR POST TYPE]',
'meta_key' => '_wholesale_price',
'orderby' => 'meta_value meta_value_num', // meta_value_num for value
'order' => 'ASC' // or DSC for low/high
);
$wp_query - new WP_Query( $args );
if ( $wp_query->have_posts() ) {
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
// Your loop
}
}

Wordpress custom taxonomy pagination not working

I’m using the WP PageNavi plugin for pagination. This particular problem in not getting the taxonomy-portflio-category.php page to paginate is also a problem when WP PageNavi is turned off.
I’ve had a heck of a time getting pagination to work on the homepage and on a page template page, but I did get them to work. Here’s their code:
page-home.php (used as a Page template on a static front page called “Home”)
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => $paged, 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
page-portfolio.php (used as a Page template on a Page called “Work”)
$i = 0;
$loop = new WP_Query( array( 'post_type' => 'portfolio', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 24 ) );
while ( $loop->have_posts() ) : $loop->the_post();
// output
$i++; endwhile;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi( array( 'query' => $loop ) );
wp_reset_postdata();
}
Pagination works!
taxonomy-portfolio-category.php (used as a way to display portfolio sections e.g. print, photography, etc.)
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
global $wp_query;
query_posts( array_merge( $wp_query->query, array( 'posts_per_page' => 2 ) ) );
if (have_posts()) : while ( have_posts() ) : the_post();
// output
endwhile; endif;
if ( function_exists( 'wp_pagenavi' ) ) {
wp_pagenavi();
}
Page 1 (/portfolio/interactive/) looks great! It’s definitely only posting 2 items and it calculates the correct number of pagination pages. But when you click on page 2 or 3 or 4 (/portfolio/interactive/page/2/) the site defaults to index.php and shows “Page not found”. Pagination fails!
Hopefully I can resolve this soon. I’ve seen A LOT of people with this same problem of pagination on custom taxonomy pages, but no solid solutions. Please help!
You Need to set posts per page to 24 on the Settings -> Reading page in WP admin. Hope this helps someone.
I have tried using WP-Pagenavi but it never worked so i used the pagination from Wordpress it self, i used the twentyfourteen_paging_nav() function form Twentyfourteen becuse it has a taxonomy page, here is the code:
if ( ! function_exists( 'twentyfourteen_paging_nav' ) ) :
function twentyfourteen_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'twentyfourteen' ),
'next_text' => __( 'Next →', 'twentyfourteen' ),
) );
if ( $links ) :
?>
<nav class="pagination-contaner" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'twentyfourteen' ); ?></h1>
<ul class="pagination">
<?php echo $links; ?>
</ul><!-- .pagination -->
</nav><!-- .navigation -->
<?php
endif;
}
endif;
I ran into similar issue, it took me hours of googling! I found the solution at last.
Add the following code to functions.php in your theme folder:
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
if ( is_tax( 'portfolio-category') ) {
return 2;
} else {
return $option_posts_per_page;
}
}
URL for the solution
May be you need to enable search to enable pagination
While declaring custom taxonomy you should disable search excluding.
exclude_from_search => false
This fixed my problem.
I would like to share following solution (add this code to functions.php in your theme):
function fix_taxonomy_pagination ( $query ) {
// not an admin page and it is the main query
if (!is_admin() && $query->is_main_query()){
if(is_tax()){
// where 24 is number of posts per page on custom taxonomy pages
$query->set('posts_per_page', 24);
}
}
}
add_action( 'pre_get_posts', 'fix_taxonomy_pagination' );
source

Resources