We have a site with over 1200 sku's that are wrong and we want to delete them all without affecting the products.
Is there a quicker way to do this via the db in phpmyadmin.
Any help would be great.
Thanks
Here is your solution. change post per page argument and post meta values for sku. (Not tested)
https://www.themelocation.com/how-to-updateadd-sku-of-all-products-in-woocommerce/
add_action( 'init', 'update_sku', 10, 1);
function update_sku( $sku ){
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$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','');
$i++;
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
}
Related
I am using this code, I wanted to display 5 post on loop on home page but without changing homepage setting to "latest posts".
...
<?php if ( is_home() ) {
$query = new WP_Query( 'cat=-3,-8' );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
} else {
...
?>
...
Try out this code in your theme's functions.php.
Use the shortcode. [show_latest_posts] in block editor Or <?php do_shortcode('[show_latest_posts]'); ?>.
add_shortcode('show_latest_posts', 'zillion_latest_post_shortcode', 10);
function zillion_latest_post_shortcode(){
ob_start();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page'=>5,
'order'=>'DESC',
'orderby'=>'ID',
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<h3>'.get_the_title().'</h3>';
echo '<div>'.get_the_content().'</div>';
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$contents = ob_get_clean();
return $contents;
}
I have a custom field in post_meta table of wordpress database. It's called 'combined'. I want to seperate the values based on key and save them as multiple rows if there are multiple values for the key.
My meta_key:
Result after I run my code:
What I want:
My Code:
add_action( 'init', function() {
if ( 'migrate' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
$query = new WP_Query( [
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'any',
] );
if ( ! $query->have_posts() ) {
return;
}
while ( $query->have_posts() ) {
$query->the_post();
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
update_post_meta( get_the_ID(), $singleData, $value );
}
}
} );
I have found the way to get the values as I want them to be in the database. Changing the loop part in the above code with code did the trick.
$data = get_post_meta( get_the_ID(), 'combined', true );
foreach($data as $singleData => $value ){
$thisArray = $data[$singleData];
foreach ($thisArray as $key2 => $value){
add_post_meta( get_the_ID(), $singleData, $thisArray[$key2], $unique = false);
}
}
So I'm trying to get pagination correct for my wordpress site so categories and pages display the correct posts :
wp_reset_query();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
I get the posts to display but the following parameters :
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
does not seem to work... any idea why ?
Chances are you don't need wp_reset_query() at the top unless you are overriding the global $wp_query somewhere else in your code.
When you are creating a custom query you need to use that throughout your loop. In your case that is $query so we need to use that when calling have_posts(), the_post(), and max_num_pages
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$sticky = get_option( 'sticky_posts' );
$args = array(
'ignore_sticky_posts' => 1,
'posts_per_page' => 10,
'orderby' => 'date',
'post__not_in' => $sticky,
'paged' => $paged,
);
$query = new WP_Query( $args );
?>
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
// interact with the post here
<?php endwhile; ?>
<?php endif; ?>
// we overwrote the global $post when called the_post(); so we need to reset that
<?php wp_reset_postdata(); ?>
// again we need to reference our custom query for our pagination
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages
) );
?>
I'm trying to get this code to pull only the "stills" category on my Wordpress page. I tried a few different things but it just pulls all of the category thumbnails.
<?php
$args = array( 'numberposts' => '12' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
if ( has_post_thumbnail($recent["ID"])) {
echo '<li>' . get_the_post_thumbnail($recent["ID"], 'thumbnail') . '</li>';
}
}?>
You can try:
<?php
$args = array( 'numberposts' => '12' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
if ( has_post_thumbnail() ) {
if (is_category('stills')){
the_post_thumbnail( 'thumbnail' );
}
}
}?>
In wordpress, the following function will echo out a list of categories with the posts associated with each category underneath each category name.
This works fine, except for the fact that this produces a flat structure. Some of the categories are child categories other categories, and I'd like to be able to output a list with a structure that matches this (kind of like a site map)
Is anyone able to help me figure out how to modify this code to achieve this?
function posts_by_category() {
//get all categories then display all posts in each term
$taxonomy = 'category';
$param_type = 'category__in';
$term_args=array(
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) { ?>
<div class="category section">
<h3><?php echo ''.$term->name;?></h3>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
</div>
<?php
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
}
Here is a code snippet I found the other day Hierarchical Category List with Post Titles, should do the trick.
<?php
/*****************************************************************
*
* alchymyth 2011
* a hierarchical list of all categories, with linked post titles
*
******************************************************************/
// http://codex.wordpress.org/Function_Reference/get_categories
foreach( get_categories('hide_empty=0') as $cat ) :
if( !$cat->parent ) {
echo '<ul><li><strong>' . $cat->name . '</strong></li>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat ) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
if( $cat_posts ) :
foreach( $cat_posts as $post ) :
echo '<li>';
echo '' . $post->post_title . '';
echo '</li>';
endforeach;
endif;
$next = get_categories('hide_empty=0&parent=' . $cat);
if( $next ) :
foreach( $next as $cat ) :
echo '<ul><li><strong>' . $cat->name . '</strong></li>';
process_cat_tree( $cat->term_id );
endforeach;
endif;
echo '</ul>';
}
?>