Reputation: 99
Im using the below code in order to add all Wordpress posts (excluding the 'sliders' category) to a category called 'Frontpage' ID = 28
function add_category_automatically1($post_ID) {
global $wpdb;
$postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
foreach ($postsWeWants as $postsWeWant) {
if (!in_category('sliders')) {
$cat = array(28, );
wp_set_object_terms($post_ID, $cat, 'category', true);
}
}
I want to add the exception of an additional category called 'business-information' but I can't get the OR operator to validate properly.
I was looking at using something like below
function add_category_automatically1($post_ID) {
global $wpdb;
$postsWeWants = $wpdb->get_results("SELECT ID, post_author FROM $wpdb->posts where ID = $post_ID");
foreach ($postsWeWants as $postsWeWant) {
if (!in_category('sliders')) OR (!in_category('business-information')) {
$cat = array(28, );
wp_set_object_terms($post_ID, $cat, 'category', true);
}
}
Upvotes: 1
Views: 64
Reputation: 4202
You are using it wrong instead if (!in_category('sliders')) OR (!in_category('business-information')) {
write it like this (one more thing which is mentioned by @FirstOne you need to use AND
instead OR
to apply both conditions not one of them)
if( !in_category('sliders') AND !in_category('business-information') ) {
...
}
so that both !in_category
checks will be in same if( ... )
scops
Upvotes: 1
Reputation: 54841
This:
if (!in_category('sliders')) OR (!in_category('business-information'))
// ^ -- ^ -- and here
is wrong. Because you close )
and open (
too early.
Proper code is:
if (!in_category('sliders') OR !in_category('business-information'))
And by the way such logic is invalid. If item is in 'business-information'
then !in_category('sliders')
true. I suppose you need to check for not existing in both cats:
if (!in_category('sliders') AND !in_category('business-information'))
Upvotes: 1