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?
Related
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' )
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.
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 have several John Jones with different names in a form. I submit the form to another program which uses "$checkednames = implode(', ', $_POST['raters']);". I echo $checkednames and see all the names but "count($checkednames)" is 1 and not the number of names. What could be wrong?
I appreciate any help.
You may seen the results by going to:
www.golfcourseratingassistant.org/ratecourse/
select Course name > Select Tee Box > Course Data ...select for all lists then "Save Data".
Selected data is only valid for the current session.
It will be 1 only, implode() returns a string containing a string representation of all the array elements in the same order, with the glue string between each element. See Manual
explode() will return an array of strings
So count() after explode() will give you the number of elements.
You can see the names because it is one string.
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
echo count($comma_separated);// Output will be 1
For explode():
$string= "lastname,email,phone";
$array= explode(",", $string);
echo count($array); //output will be 3