get_terms orderby numeric

You can use usort to sort the object $children. The function to sort after you got the $children object can be like this: usort( $children, function($a, $b) { $ai = filter_var($a->name, FILTER_SANITIZE_NUMBER_INT); $bi = filter_var($b->name, FILTER_SANITIZE_NUMBER_INT); if ($ai == $bi) { return 0; } return ($ai < $bi) ? -1 : 1; });

Invalid argument supplied for foreach() in search.php

Because get_the_terms will not always return an array; it will be false if the post has no terms, or a WP_Error if the taxonomy does not exist – always sanity check the result before looping: $terms_slugs = array(); $terms = get_the_terms( $post->id, ‘type’, /* Note: get_the_terms does not take a 3rd argument ==> */ array( … Read more

Limiting woocommerce line_total decimal length

Just round the decimal to two digit as per your requirement. $myID = 2859; $myorder = new WC_Order($myID); foreach ($myorder->get_items(‘line_item’) as $item) { $rounded_total = round($item[“line_total”],2);// allow 2 decimal and avoid further $Data = $Data.OrderLine($item[“name”], $rounded_total, $item[“line_tax”], $item[“qty”], “piece”, “x”); }

Show only one category

CHRISTMAS EDITION Due to Christmas time and my brain hitting some serious technical difficulties, I got get_the_category() and get_the_categories filter mixed up. Here is the revised filter function, working: You can also make use of the get_the_categories filter add_filter( ‘get_the_categories’, function ( $categories ) { // Only return the first object in the array return … Read more

Set post title based on first h2 element in the content section

If you can handle this in the PHP side, you might be able to pull the h2 tags from the content using regular expression. PHP VERSION Test content $content = “<h2>This is H2 Content</h2><p>This is p content</p>Random Content<h2>This is another H2 section</h2><b><h2>This the third H2 section</h2></b>”; Grab any text inside H2s preg_match_all(‘#<h2>(.*?)</h2>#’, $content, $matches); // … Read more

PHP code inside shortcodes

You can not put PHP functions inside your shortcode. However, you do not need to. You can just use your foreach-loop to create the html and store it in a variable, for example $shortcodehtml. So, afterwards, you can call echo do_shortcode(‘[O_U user_name=”operator” blocked_message=”This page is restricted for guests.”]’ . $shortcodehtml . ‘[/O_U]’); The better version … Read more