WordPress Update meta_value by meta_key
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Clear Stock
function clear_current_stock(){
// Declare the $wpdb var
global $wpdb;
// Run My SQL Query (My Query is setting the meta value for stock to zero)
$wpdb->query("UPDATE wp_postmeta SET meta_value = 0 WHERE meta_key = '_stock'");
// Check if there were any errors or if my query was successful.
if ( is_wp_error( $wpdb ) ) {
echo $wpdb->get_error_message();
}
else {
echo 'true';
}
}
// Clear Stock function clear_current_stock(){ // Declare the $wpdb var global $wpdb; // Run My SQL Query (My Query is setting the meta value for stock to zero) $wpdb->query("UPDATE wp_postmeta SET meta_value = 0 WHERE meta_key = '_stock'"); // Check if there were any errors or if my query was successful. if ( is_wp_error( $wpdb ) ) { echo $wpdb->get_error_message(); } else { echo 'true'; } }
// Clear Stock
function clear_current_stock(){ 
// Declare the $wpdb var
    global $wpdb;
// Run My SQL Query (My Query is setting the meta value for stock to zero)
    $wpdb->query("UPDATE wp_postmeta SET meta_value = 0 WHERE meta_key = '_stock'");
// Check if there were any errors or if my query was successful.
    if ( is_wp_error( $wpdb ) ) {
         echo $wpdb->get_error_message();
    }
    else {
         echo 'true';
    }
}

Then you can call the function like this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php clear_current_stock(); ?>
<?php clear_current_stock(); ?>
<?php clear_current_stock(); ?>
Check User Role ( if statement included )
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$user_id = get_current_user_id();
if (!empty($user_id)) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
// echo $role[0];
if ($role[0] == 'free-client' OR $role[0] == 'administrator') {
//code here...
}
}
?>
<?php $user_id = get_current_user_id(); if (!empty($user_id)) { $user = wp_get_current_user(); $role = ( array ) $user->roles; // echo $role[0]; if ($role[0] == 'free-client' OR $role[0] == 'administrator') { //code here... } } ?>
<?php
  $user_id = get_current_user_id();
  if (!empty($user_id)) {
  $user = wp_get_current_user();
  $role = ( array ) $user->roles;
  // echo $role[0];
  if ($role[0] == 'free-client' OR $role[0] == 'administrator') {
    //code here...
  }
  }
?>
PHP Delete Cookie
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<?php
echo "Cookie 'user' is deleted.";
?>
<?php // set the expiration date to one hour ago setcookie("user", "", time() - 3600); ?> <?php echo "Cookie 'user' is deleted."; ?>
<?php
// set the expiration date to one hour ago
  setcookie("user", "", time() - 3600);
?>

<?php
  echo "Cookie 'user' is deleted.";
?>
PHP Set Cookie and Retrieve Cookie
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$cookie_name = "user";
$cookie_value = "Alex Porter";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_name];
}
?>
<?php $cookie_name = "user"; $cookie_value = "Alex Porter"; setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); ?> <?php if(!isset($_COOKIE[$cookie_name])) { echo "Cookie named '" . $cookie_name . "' is not set!"; } else { echo "Cookie '" . $cookie_name . "' is set!<br>"; echo "Value is: " . $_COOKIE[$cookie_name]; } ?>
<?php
  $cookie_name = "user";
  $cookie_value = "Alex Porter";
  setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>

<?php
  if(!isset($_COOKIE[$cookie_name])) {
       echo "Cookie named '" . $cookie_name . "' is not set!";
  } else {
       echo "Cookie '" . $cookie_name . "' is set!<br>";
       echo "Value is: " . $_COOKIE[$cookie_name];
  }
?>
WordPress Add Custom Post Type With Category
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
// Custom Post 1
register_post_type( 'courses',
array(
'labels' => array(
'name' => __( 'Courses' ),
'singular_name' => __( 'Course' )
),
'public' => true,
'has_archive' => true,
'supports' => array('title', 'editor', 'thumbnail'),
'show_ui' => true
)
);
register_taxonomy('courses_categories', 'courses', ['label' => 'Courses Categories','hierarchical' => true]);
// Custom Post 1 register_post_type( 'courses', array( 'labels' => array( 'name' => __( 'Courses' ), 'singular_name' => __( 'Course' ) ), 'public' => true, 'has_archive' => true, 'supports' => array('title', 'editor', 'thumbnail'), 'show_ui' => true ) ); register_taxonomy('courses_categories', 'courses', ['label' => 'Courses Categories','hierarchical' => true]);
// Custom Post 1
    register_post_type( 'courses',
        array(
            'labels' => array(
                'name' => __( 'Courses' ),
                'singular_name' => __( 'Course' )
            ),
        'public' => true,
        'has_archive' => true,
        'supports' => array('title', 'editor', 'thumbnail'),
        'show_ui' => true
        )
    );
 register_taxonomy('courses_categories', 'courses', ['label' => 'Courses Categories','hierarchical' => true]);

NB! (The hierarchical array item is Important)
,’hierarchical’ => true

Search

Your Favourite Posts

  • Your favorites will be here.

Latest Content

© Garth Baker 2025 All rights reserved.

Pin It on Pinterest

Garth Baker
en
af
sq
am
ar
hy
az
eu
be
bn
bs
bg
ca
ceb
ny
zh-CN
zh-TW
co
hr
cs
da
nl
en
eo
et
tl
fi
fr
fy
gl
ka
de
el
gu
ht
ha
haw
iw
hi
hmn
hu
is
ig
id
ga
it
ja
jw
kn
kk
km
ko
ku
ky
lo
la
lv
lt
lb
mk
mg
ms
ml
mt
mi
mr
mn
my
ne
no
ps
fa
pl
pt
pa
ro
ru
sm
gd
sr
st
sn
sd
si
sk
sl
so
es
su
sw
sv
tg
ta
te
th
tr
uk
ur
uz
vi
cy
xh
yi
yo
zu
Share This