I wrote a function where two insert query have. One is executed and inserting data properly. But next one is not executing. And I cant check the value i want to insert if it is set or not. How to do the stuff? EXPERT's have a look kindly. My function is given below:
add_action( 'save_post', 'cs_product_save' );
function cs_product_save( $post_id ){
global $wpdb;
$cs_product_array = $_POST['cs_product'];
$cs_product_count = count($cs_product_array);
$event_start_date = $_POST['event_start_date'];
$event_end_date = $_POST['event_end_date'];
$event_start_time = $_POST['event_start_time'];
$event_end_time = $_POST['event_end_time'];
$event_all_day = $_POST['event_all_day'];
$event_phone = $_POST['event_phone'];
$event_location = $_POST['event_location'];
$event_map = $_POST['event_map'];
$table_cause_product = "wp_cause_woocommerce_product";
$table_event_info = "wp_cause_info";
for( $i=0; $i < $cs_product_count; $i++ ){
$wpdb->insert($table_cause_product,array(
'cause_ID'=>$post_id,
'product_ID'=>$cs_product_array[$i],
'status'=>'1'
),array('%d','%d','%d'));
}
$wpdb->insert($table_event_info,array(
'cause_ID'=>$post_id,
'event_start_date'=>$event_start_date,
'event_end_date'=>$event_end_date,
'event_start_time'=>$event_start_time,
'event_end_time'=>$event_end_time,
'event_all_day'=>$event_all_day,
'event_phone'=>$event_phone,
'event_location'=>$event_location,
'event_map'=>$event_map
),array('%d','%s','%s','%s','%s','%d','%s','%s','%d'));
}
I don't see any problem here with your code. But be sure to double check your code.
The issues my be with the name of your database table names. Are you sure that $table_cause_product and $table_event_info holds the actual name of the tables? I would recommend to use $wpdb->prefix instead of harcoding table names.
In my case I would check the function in several parts.
Check the $_POST actually holds the data I want.
Use $result = $wpdb->insert( $table, $data, $format ); in all cases for debug purpose as the $result would hold the result of the operation. If its is false then I would be sure that there is definitely something wrong with the operation.
Finally I would use a wp_die() (though its not a standard way to do, but it suffices my purpose) so that I can see the dumped variable data.
One major issue you might face with your code that if the post is edited after saving then it might insert another row for same post data. Again if the post is being autosaved, you need some safeguard. I would recommend a where clause here to check the row already exists or not. If exists then you can simply update the row, or else insert the data.
Hope this might help you.
Related
I made this code to below. But it isn't working. Its giving an array.
Can somebody help me with this?
<?php
/**
*$fields['listcheckbox_1574292451270']
*/
$Extra_1 = $fields['listcheckbox_1574292451270'];
if ($Extra_1 == "1_1") {
echo ("Correct");
} elseif ($Extra_1 == "2_2") {
echo ("True");
} else {
echo ("False");
}
?>
I tried to put at the last else echo = "$1_extra"; but that gave an Array.
$fields[listcheckbox_1574292451270] is a checkbox with multiple options "1_1" & "2_2"
If this code isn't any good, can somebody help me with it?
Thanks
shouldn't that first line be:
$1_extra = $fields['listcheckbox_1574292451270'];
I think you missed the ' when accessing that array.
Edit: just noticed a few things:
if echo $1_extra; outputs Array - to see the actual values of it you either need to convert the array to a string or iterate over it. For debugging array values you can use var_dump($1_extra); which provides more information than echo does.
After that you should also see how to access the data inside your array. You can read more about working with arrays in php here: https://www.php.net/manual/en/language.types.array.php
I have a buddypress site and I have edited the activity stream to include the activities I want to see however it's more logical to filter out the unwanted so future plugins don't have to be manually included I have tried != but does not work.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ).'&action=bbp_reply_create,last_activity,gmw_location,activity_liked,rtmedia_update,new_avatar,updated_profile,joined_group,new_blog_post,bbp_topic_create,created_group' ) ) : ?>
And here is what I tried.
<?php if ( bp_has_activities( bp_ajax_querystring( 'activity' ).'&action!=friendship_created,new_member' ) ) : ?>
But the attempt simply left the whole stream blank.
I've never used Buddypress, but from what I see you cannot simply use the != operator. You are placing the != in a string that is the input for a function named bp_ajax_querystring(). From the function's name, and the rest of the input, it looks like a set of GET parameters.
GET parameters only allow the setting of attributes (so != has no meaning). Instead, place the ! in front of the expression being evaluated by the if statement:
if ( !bp_has_activities( bp_ajax_querystring( 'activity' ).'&action=bbp_reply_create,last_activity,gmw_location,activity_liked,rtmedia_update,new_avatar,updated_profile,joined_group,new_blog_post,bbp_topic_create,created_group' )
Is it possible to fetch a post with content under 140 characters or 25 words ?
if possible how to do it
here is my random post code
// Random post link
function randomPostlink(){
$RandPostQuery = new WP_Query(array('post_type'=>array('tip'),'posts_per_page' => 1,'orderby'=>'rand'));
while ( $RandPostQuery->have_posts() ) : $RandPostQuery->the_post();
echo the_permalink();
endwhile;
wp_reset_postdata();
}
Character count is easy, you can just add the condition AND CHAR_LENGTH(post_content) < 140 to your where clause.
Word count is more difficult because there is no built in MySQL function for counting words. You can find simple solutions that don't work in every use case as well as complete solutions that use stored functions. I'll use a simple solution for the sake of example.
What you need to do is add a filter to the where clause and apply your additional conditions there:
add_filter( 'posts_where', 'venki_post_length_limit' );
function venki_post_length_limit($where = '') {
remove_filter( 'posts_where', 'venki_post_length_limit' );
$where .= ' AND (
CHAR_LENGTH(post_content) < 140 OR
(LENGTH(post_content) - LENGTH(REPLACE(post_content, ' ', ''))+1) < 25
) ';
return $where;
}
Notice that I remove the filter as soon as the function is called. This is so you don't apply this same condition to every query.
You should also be aware that both of those conditions are costly compared to a simple lookup on a column value (especially the word count). Neither can utilize indexes. If you have a large number of posts you may run into performance issues if you're running this query frequently. A better solution might be to calculate the word and character count when the post is created/updated and store that as meta data.
I am very baffled by this problem.
The below is the very simple function to update a column by adding some more value:
public function add_user_to_new_post_sub($email, $sub_post_type) {
global $wpdb;
$add_setting = "|||".$sub_post_type;
//echo $add_setting; exit;
$wpdb->query(
"UPDATE $this->subscriptions_table
SET subscription_settings = concat(subscription_settings, '$add_setting')
WHERE user_key = '$key'"
);
}
For some reason, the $sub_post_type is always added 2 times. As an example, if the subscription_settings column has apple in it, and $sub_post_type = orange, the end result after the query would be apple|||orange|||orange. I don't understand why the extra value being added. I even did a sanity check with echo to make sure I am not passing things twice, and I am not.
Please help, I have been struggling for some time now.
I found my reason; the function was being called again through another if statement. doh
I've got a number of custom fields in a WP theme, and I want to check if they have values - but would like a short cut to check multiple values at once - something like this:
if ( get_post_meta ( $post->ID, "first_value", "second_value", "third_value", $single = true) !="") :
// do stuff here, as they are all set ##
else:
// do something else, as they are not all set ##
endif;
this does not throw an error, but it only checks if the first value is set - any ideas?
First note the affects of setting the $single variable to true:
If $single is set to false, or left
blank, the function returns an array
containing all values of the specified
key. If $single is set to true, the
function returns the first value of
the specified key as a string, thus you can use string compare (not in an array)
Solution 1: That being said you could then use $single=false to get an array of values, and then compare the array of values to an array of null values.
Solution 2: Or you could use several conditions in the if_else statement:
if ( get_post_meta($post->ID,"first_value",true)!="" && get_post_meta($post->ID,"second_value",true)!="" && get_post_meta($post->ID,"third_value",true)!="") :
// do stuff here, as they are all set ##
else:
// do something else, as they are not all set ##
endif;
Solution 3: You could also use nested if_then statements if you prefer those.
The question is, which solution works best for you?