Create shortcodes within foreach loop (using array)

Solved issue of dynamic function name by using $shortcode var as function name foreach ($shortcodes as $shortcode) { add_shortcode($shortcode, function ($atts) use ($shortcode) { $filepath = str_replace(‘_’, ‘-‘, $shortcode); ob_start(); require_once(get_stylesheet_directory() . ‘/template-parts/’ . $filepath . ‘.php’); return ob_get_clean(); }); }

how can i show only the parents in owl-carousel?

According to the documentation, you would have to use post_parent => 0 So your code would look like this: <div class=”owl-carousel owl-theme”> <?php $c = 0; $q2 = new WP_Query( array( ‘post_type’ => array( ‘post’, ‘book’ ), ‘post_status’ => ‘publish’, ‘post_parent’ => 0, //add this here ‘orderby’ => ‘modified’, ‘order’ => ‘desc’ ) ); while … Read more

tax_query operator woes

I’m not sure that there is a way to get it to use ‘OR’ instead of ‘AND’. Alternatively, you can do: $myquery[‘tax_query’] = array( ‘relation’ => ‘OR’, array( ‘taxonomy’ => ‘regions’, ‘terms’ => array(‘region1’), ‘field’ => ‘slug’, ‘operator’ => ‘IN’ ), array( ‘taxonomy’ => ‘population’, ‘terms’ => array(‘pop1’), ‘field’ => ‘slug’, ‘operator’ => ‘IN’ ), … Read more

WordPress Plugin Dev: Using array for WP options

If you are using the Settings API then you don’t have to save the options, that’s done for you. So when using an array to store the options your validation function should get an array of all existing options, update only the changed and return that array. Something like this: function my_settings_option_validate( $input ) { … Read more

Working with query_posts ( arrays and query strings)

I’d suggest not using $query_string to simplify things. If you’re using an array, stick with the array form for the query variables: global $wp; $paged = ((int)get_query_var(‘paged’)) ? (int)get_query_var(‘paged’) : 1; $s_array = array( ‘post_type’ => ‘blog’, ‘caller_get_posts’ => 1, ‘paged’ => $paged, ‘meta_query’ => array( array( ‘key’ => ‘votes_percent’, ‘value’ => ’50’, ‘compare’ => … Read more

Passing Custom Field Data as Array to be Saved (Resulting Custom Field Array is inconsistent)

Definite solution: Seeing as wp’s core get_post_custom function returns multiple data fields saved as a single key in a random order, I resolved the issue by writing a function to replace it – which has some added flexibility: /*————————————————————— Function: Retrieve post meta field for a post based on ID Usage: [admin/meta_box/downloads.php] = to replace … Read more

PHP get the first post separately from array returned by wpdb->get_results()

once you have set of result in particular order you can achieve it simply as below : – $pageposts=$wpdb->get_results(” SELECT * FROM $wpdb->posts WHERE post_type=”post” AND post_status=”publish” ORDER BY post_date DESC LIMIT 6 “, OBJECT); First time $i=1; foreach($pageposts as $pageposts_first){ //here goes your first post if($i==1) break; } For rest of posts $i=0; foreach($pageposts … Read more