Bulk wpdb insert for all posts [Wordpress] - php

I've managed to save a string into Wordpress database. The problem is that I am only able to insert the string when this code runs in a single url. I've got around 1500 posts in my database so I'd like to make it work for all of them.
global $wpdb;
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
I'd like to make this insert in all the posts without having to load every single url. I suppose I should do that via functions.phpbut I haven't found a proper way to do it.

Yes, you can achieve this by putting this code into functions.php , but this will insert all post every time site loads, so add this once and remove after complete execution.
<?php
global $wpdb;
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
$ratingvalue = get_post_meta($post->ID,'ratingpost',true);
$result = $wpdb->replace(
$wpdb->postmeta,
array(
'post_id' => $post->ID,
'meta_key' => 'ratingpost',
'meta_value' => $ratingvalue
),
array(
'%s',
'%s',
'%s'
)
);
<?php endforeach;
wp_reset_postdata();?>

Just use SQL for this:
global $wpdb;
$updated = $wpdb->query( UPDATE {$wpdb->postmeta} pm, {$wpdb->posts} p SET pm.meta_value = {$ratingvalue} WHERE pm.meta_key = 'ratingpost' AND p.ID = pm.post_id );

Related

How to get all products that doesn't have feature image in woocommerce?

some of my products missing their images, what I'm trying to do is to make a query that will give me only products with the default image (woocommerce place holder image).
this is what i have tried :
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query', array(
array(
'key' => '_thumbnail_id',
'value' => '5',
'compare' => '=='
)
)
);
i found place holder image id using this function :
attachment_url_to_postid("/wp-content/uploads/woocommerce-placeholder.png");
the query returns every single product I have, and not only those with the placeholder image, what causes it, and is there a better way?
You can check using meta_key _thumbnail_id. Try the below query.
global $wpdb;
$post_ids = $wpdb->get_results( "
SELECT ID FROM $wpdb->posts
WHERE ID NOT IN (
SELECT post_id from $wpdb->postmeta
WHERE meta_key = '_thumbnail_id'
)
AND post_type = 'product'
AND post_status = 'publish'
" );
echo "<pre>"; print_r( $post_ids ); echo "</pre>";

Wordpress: Show three random posts on every page refresh

In my WordPress, I am able to show one random post by using the below code:
global $post;
if ( post_type_exists( 'testimonial' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'testimonial',
'orderby' => 'rand',
'posts_per_page' => -1
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
// do something with post - e.g. the_excerpt(), the_content(), etc.
}
// Restore original post data
wp_reset_postdata();
}
Ref: https://barn2.co.uk/how-to-display-a-random-post-in-wordpress/
Help me to show 3 random posts by using the above method.
Can you try using in array property or WP_Query. A sample code is given belwo
$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type` = 'testimonial' ORDER BY RAND() LIMIT 3" );
$ids = array();
foreach($results as $$result){
$ids[] = $results->ID;
}
$args = array(
'post_type' => array( 'testimonial' ),
'orderby' => 'ASC',
'post__in' => $ids
);
$testimonial_query = new WP_Query( $args );

WP->Query get Custom Post ID from meta_value & meta_key

I have been trying for a couple of hours to make this work - but for some reason its simply to difficult for me. I have a custom post_type 'house', and I want to find the post_id of my custom post_type with a meta_key and certain meta value.
Lets say i want to find post_id for a house with
meta_key='house_id'
meta_value='231sd1223'
How exactly would i do that with wp->query?
Here you have the query even with a loop. However, querying meta values is making more DB queries, consider looping throught "house" post type and than doing something only if meta_value is equal the house number.
// WP_Query arguments
$args = array (
'post_type' => array( 'house' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'house_id',
'value' => '231sd1223',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
global $wpdb;
$results = $wpdb->get_results( "select post_id, meta_key from $wpdb->postmeta where meta_value = '231sd1223.'", ARRAY_A );

Get all posts from Wordpress as JSON

I am trying ti get all the posts form my Wordpress website with the post_type "product".
I tried the below but it doesn't work.
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish');
$loop = new WP_Query( $args );
$array = array();
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$array[] = array(
'id' => get_the_ID(),
'title' => get_the_title()
);
endwhile;
wp_reset_query();
ob_clean();
echo json_encode($array);
exit();
?>
Although when I add 'posts_per_page' => 450 to $args, it returns posts of the type, however if I add a value higher than 450 such as 500, it returns nothing again.
I am using this to loop through all product posts and add the name, price etc. to an array in the loop.
How do I fix the $args to get all the product posts.
EDIT:
I also recently tried:
<?php
$args="SELECT * FROM wp_posts WHERE wp_posts.`post_type` = 'product' AND wp_posts.`post_status` = 'publish'";
$loop = get_results( $args );
$array = array();
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$array[] = array(
'id' => get_the_ID(),
'title' => get_the_title()
);
endwhile;
// wp_reset_query();
ob_clean();
echo json_encode($array);
exit();
?>
No need to simulate the loop here. This is the most straightforward way:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'nopaging' => true
);
$query = new WP_Query( $args ); // $query is the WP_Query Object
$posts = $query->get_posts(); // $posts contains the post objects
$output = array();
foreach( $posts as $post ) { // Pluck the id and title attributes
$output[] = array( 'id' => $post->ID, 'title' => $post->post_title );
}
echo json_encode( $output );
Or you can leave out the foreach and just echo json_encode( $posts ); to get all the properties of the posts.
please check your table for data. Is there any post of post type products. if yes then the simple query should work
SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish'
you can run this code in phpmyadmin directly. and see if any post is there. and please replace $wpdb with the prefix of you tables.
<?php
//this file is in main folder and it works for me(yourWordpressWebsite.com/yourFile.php)
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
global $wpdb;
$posts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post'");
echo json_encode($posts);
exit();
?>
output:
[{"ID":"1","post_title":"Hello world!"},{"ID":"63","post_title":"Blockquote Post"}...
This is how you show all posts in a query: 'posts_per_page' => -1 - so your query becomes:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
As far as i know, you need to get posts with manual query,
Just like this,
global $wpdb;
$args = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish' ORDER BY $wpdb->posts.`ID`";
$loop = $wpdb->get_results( $args );
In my experience, 'posts_per_page' => -1 will not returns all the posts, i don't know why.
Somehow wordpress not returning more than 500 or 1000 posts from database...
You might try using a plugin developed for such a purpose. This one is listed on the WordPress.org website, and it seems like it may help you.
JSON API
It's always best to check the WordPress.org hosted plugins first
$args = array( 'posts_per_page' => -1, 'post_type' => 'product' );
$myposts = get_posts( $args );
echo json_encode($myposts);
I think this should work

$wpdb query not running

I'm working on a wpdb query and it's just not running. And for some reason the error reporting for wpdb isn't giving me any errors.
So, can anyone spot an obvious mistake in this query?
I'm attempting to get the meta_value of all _sell_media_attached_file keys in the post_meta table.
I'm first running a wp_query , getting the post ID's and then running each post ID through the wpdb query.
Here's what I'm working with:
// run the loop
$loop = new WP_Query( array(
'post_type' => 'sell_media_item',
'collection' => $club,
'include_children' => false,
'year' => $year,
'monthnum' => $month,
'day' => $day,
'fields' => 'ids',
) );
if ( $post_ids = $loop->get_posts() ) {
$post_ids = implode( ',', $post_ids );
//breaks here
$atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id = $post_ids AND meta_key = '_sell_media_attached_file' " );
echo('<b>query:</b> <pre>'); var_dump($atts_ids);
But as I say, nothing get's output in the var_dump and if I add $wpdb->print_error(); I get no errors.
In your piece of code i didn't see the initialization of wpdb class to access the wpdb class in your wordpress you have to define it as the global to access the functions of the class and also if $post_ids contains the multiple ids with comma separated you have to use the IN() clause instead of = operator
global $wpdb;
// run the loop
$loop = new WP_Query( array(
'post_type' => 'sell_media_item',
'collection' => $club,
'include_children' => false,
'year' => $year,
'monthnum' => $month,
'day' => $day,
'fields' => 'ids',
'nopaging'=>true
) );
if ( $post_ids = $loop->get_posts() ) {
$post_ids = implode( ',', $post_ids );
//breaks here
$atts_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE post_id IN($post_ids) AND meta_key = '_sell_media_attached_file' " );
echo('<b>query:</b> <pre>'); var_dump($atts_ids);
why not try something like that?
if( $loop->have_posts() ){
$ids = $loop->get_posts();
foreach( $ids as $id ){
$atts_ids[] = get_post_meta($id, '_sell_media_attached_file', true|false ); //true if single value false if multiple values return as array
}
}else{
//Do something else
}
Hope it helps!

Categories