I want to update a meta value with a specific meta key out of wordpress page. What I want is that, multiplying a meta value with a variable then write the result to another meta value.
for example;
$var=5
meta_key|meta_value
key-1 |10
key-2 |20
the meta_value for the key-1 = 100 (key-2 * $var). I have tried below code to retrieve key-1 but it returns nothing.
require_once("wp-load.php");
function wp_update_value( ) {
global $wpdb;
$mylink = $wpdb->get_results("
SELECT $wpdb->postmeta.meta_value
FROM $wpdb->postmeta
WHERE $wpdb->postmeta.meta_key = 'key-1'",
ARRAY_A);
$array = unserialize($mylink);
return $array;
}
How can I get two meta values by meta keys and update them for all posts?
You can do it inside a loop, something like:
<?php
header('Response: HTTP/1.1 200 OK');
define('WP_USE_THEMES', false);
require('../../../../wp-load.php');
$args = array(
'posts_per_page' => '-1',
);
$the_query = new WP_Query( $args );
$var = 5;
if ( $the_query->have_posts() ){
while ( $the_query->have_posts() ) : $the_query->the_post();
$value = 100 * (get_post_meta(get_the_ID(),'key-2',true) * $var);
update_post_meta(get_the_ID(), 'key-1', $value);
endwhile;
}
?>
Related
I'm developing wordpress plugin.
I need to find out post_id from thumbnail_id(not reverse !).
How can I do this?
You can get result by this code
global $wpdb;
$_thumbnail_id = {thumbnail id};
$sql = "SELECT `post_id` FROM `wp_postmeta` WHERE `meta_value` = $_thumbnail_id";
$result = $wpdb->get_results( $sql, ARRAY_A );
//access first returned post id
var_dump($result[0]['post_id']);
If you added same image for multiple posts there will be multiple returns.
You can use get_the_ID() to get the post id. You can find this function in wp-includes/post-template.php
function get_the_ID() {
$post = get_post();
return ! empty( $post ) ? $post->ID : false;
}
I need a function to check if post_content already exist in database.
Wordpress built in function post_exists() checks by post post_title.
I need to check by post_content regardless of the post_title.
There such a function exist?
How can I solve this?
Thank you for your help
It looks like a small variation on post_exists() should work. Create a function like this in your child theme's functions.php, and then use it instead of post_exists():
function post_exists_by_content($content) {
global $wpdb;
$post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) );
$query = "SELECT ID FROM $wpdb->posts WHERE 1=1";
$args = array();
if ( !empty ( $content ) ) {
$query .= ' AND post_content = %s';
$args[] = $post_content;
}
if ( !empty ( $args ) )
return (int) $wpdb->get_var( $wpdb->prepare($query, $args) );
return 0;
}
Im extremely new to WordPress and WooCommerce code. I've been provided with some working code that resides in the plugins/woocommerce/templates/archive-product.php
The function is pretty simple, it simply fetches for an array of data from a remote site and makes use of the JSON returned to find the matching SKUs and inject them as items in the product list.
Works quite nicely, however, as I'm new to Woo & WP, I'm hoping someone might be able to show me how I can transform this code into the proper way of it being defined as a plugin?
I'm hoping its just a case of wrapping some additional code around the function, but I'm unsure as to where to start
any tips greatly appreciated
if ( wc_get_loop_prop( 'total' ) ) {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$cat = explode('/', $actual_link);
if ($cat[4] == 'my-product-list') {
global $current_user;
get_currentuserinfo();
$data = array( 'email' => $current_user->user_email);
$response = wp_remote_post( 'https://www.shop.com/remote-data/', array( 'data' => $data ) );
$curl = 'https://www.shop.com/remote-data/';
$response = wp_remote_get( $curl );
$rows = wp_remote_retrieve_body( $response ) ;
$decode = json_decode(stripslashes($rows), true);
global $wpdb;
$product_id = array();
$i = 0;
foreach ($decode as $single_data) {
foreach ($single_data['items'] as $data) {
$result = $wpdb->get_results ( "SELECT post_id FROM wp_postmeta WHERE meta_key = '_sku' AND meta_value = '".$data."'" );
$product_id[$i] = $result[0]->post_id;
$i++;
}
}
$product_id = array_filter(array_unique($product_id));
$args = array(
'post_type' => 'product',
'post__in' => $product_id
);
//The Query
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) {
$the_query->the_post();
do_action( 'woocommerce_shop_loop' );
wc_get_template_part( 'content', 'product' );
}
You could try to hook it into woocommerce_before_main_content
Like so:
function fetch_shop_data() {
// your code
}
add_action('woocommerce_before_main_content', 'fetch_shop_data');
I need to get all users who commented on a post. And for each user the comments. Searched online but couldn't find any solution.
If post_id= 15 then
<?php
$comments = get_comments('post_id=15');
foreach($comments as $comment) :
echo($comment->comment_author .'--coment--'.$comment->comment_content);
echo '<br>';
endforeach;
?>
see this for further details https://codex.wordpress.org/Function_Reference/get_comments
.
You need get_comments().
Here is how you get all comments on a certain post:
<?php
$args = array(
'post_id' => 0, // Place post ID here.
);
get_comments( $args );
?>
After running this code, you will get an array of posts and user IDs, you can use the IDs with a foreach to get all comments for each user. Like this:
<?php
$args = array(
'user_id' => 0, // Place user ID here.
);
get_comments( $args );
?>
You can get comment writer from its comment id:
<?php $author = get_comment_author( $comment_ID ); ?>
You can generate short code use it, Here this will display all information of comment.You need to extract which one is required.
function input_func( $atts ) {
global $post;
$comments = get_comments( array( 'number' => 2, 'post_id' => get_the_ID() ) );
print_r( $comments);
}
add_shortcode( 'input', 'input_func' );
Code:
global $wpdb, $post;
// Query
$query = sprintf("SELECT comment_author_email
FROM {$wpdb->comments}
JOIN {$wpdb->posts} ON {$wpdb->posts}.ID = {$wpdb->comments}.comment_post_ID
WHERE comment_post_ID = %d AND comment_approved = '1'",
$post->ID);
$emailslist = $wpdb->get_col($query);
$emailslist = array_unique($emailslist);
// get all emails
print_r($emailslist);
Here you can get list of emails of users.
You can also use WP_Comment_Query
I have a Custom Post Type, 'ioni_codex'
I am using built-in Wordpress category as taxonomy
I want to list all categories used by 'ioni_codex'.
I assume that this code will do the trick:
$myargs = array (
'type' => 'ioni_codex'
);
$categories = get_categories( $myargs );
However instead I see the list of all categories not the categories assigned to by 'ioni_codex'.
What Am I doing wrong?
get_categories() does not accept post_type as an argument, use taxonomy instead and refer to the taxonomy by the name you gave it when registering it. Here is a link to the codex which can explain it in more detail - http://codex.wordpress.org/Function_Reference/get_categories.
Instead of type you have to set post_type , by default get_categories try to hide empty categories , if you want display it all add hide_empty property set to false
get_categories(array(
'post_type' => 'ioni_codex',
'hide_empty' => false,
) );
I have an answer at a sister project:
Bainternet♦ has re-written get_terms() function to provide for the post_type
Please refer to his solution here or just copy and past from below:
/* get terms limited to post type
# $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
# $args - (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
# $post_type - (string|array) of post types to limit the terms to
# $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
$args = array(
'post_type' => (array)$post_type,
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
$terms = array();
while ($the_query->have_posts()){
$the_query->the_post();
$curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
foreach ($curent_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $c;
}
}
}
wp_reset_query();
//return array of term objects
if ($fields == "all")
return $terms;
//return array of term ID's
if ($fields == "ID"){
foreach ($terms as $t){
$re[] = $t->term_id;
}
return $re;
}
//return array of term names
if ($fields == "name"){
foreach ($terms as $t){
$re[] = $t->name;
}
return $re;
}
// get terms with get_terms arguments
if ($fields == "get_terms"){
$terms2 = get_terms( $taxonomies, $args );
foreach ($terms as $t){
if (in_array($t,$terms2)){
$re[] = $t;
}
}
return $re;
}
}
While quick reading #ioni's answer; it looks like it would give you a proper result it will most likely be pretty slow as it goes through every post and then looks for all terms that belong to each post individually. This will easily result in thousands of SQL queries.
My recommendation is to use a single query to get the IDs of all terms that belong to a post type. Then use those ids to filter get_terms().
MySQL query:
SELECT
wp_term_taxonomy.term_id
FROM
wp_term_relationships
LEFT JOIN wp_posts ON wp_term_relationships.object_id = wp_posts.ID
LEFT JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
WHERE
wp_term_taxonomy.taxonomy = 'category'
AND wp_posts.post_type = 'ioni_codex'
GROUP BY
term_id
PHP function:
<?php
function ioni_get_terms_by_post_type( $taxonomy = 'category', $post_type = 'post', $args = array() ) {
global $wpdb;
$sql = $wpdb->prepare(
"
SELECT
{$wpdb->prefix}term_taxonomy.term_id
FROM
{$wpdb->prefix}term_relationships
LEFT JOIN {$wpdb->prefix}posts ON {$wpdb->prefix}term_relationships.object_id = {$wpdb->prefix}posts.ID
LEFT JOIN {$wpdb->prefix}term_taxonomy ON {$wpdb->prefix}term_relationships.term_taxonomy_id = {$wpdb->prefix}term_taxonomy.term_taxonomy_id
WHERE
{$wpdb->prefix}term_taxonomy.taxonomy = '%s'
AND {$wpdb->prefix}posts.post_type = '%s'
GROUP BY
term_id
",
$taxonomy,
$post_type,
);
$term_ids = $wpdb->get_col( $sql );
if ( empty( $term_ids ) ) {
return array();
}
// custom code to allow for exclude to work
if ( ! empty( $args['exclude'] ) ) {
// allow exclude to be either a string or array
$exclude = is_string( $args['exclude'] ) ? (array) $args['exclude'] : $args['exclude'];
// filter $term_ids with array from $args['exclude']
$term_ids = array_filter( $term_ids, function( $term_id ) use ( $exclude ) {
return ! in_array( $term_id, $exclude );
} );
}
$args = wp_parse_args(
$args,
array(
'taxonomy' => $taxonomy,
'include' => $term_ids,
),
);
return get_terms( $args );
}
Usage:
<?php
$categories = ioni_get_terms_by_post_type( 'category', 'ioni_codex' );