In WordPress Recent Posts Widget , only show posts tagged with the current page url

/******* filter posts by tag based on page url ***********/ add_filter(‘widget_posts_args’, ‘wpsites_modify_recent_posts_widget’, 100000); function wpsites_modify_recent_posts_widget($params) { $url = sanitize_text_field($_SERVER[‘REQUEST_URI’]); $last_word = basename($url); error_log(“last word = $last_word”); $tags_filter_by = array(‘who’,’what’,’when’,’where’,’why’,’how’); // only do for these pages if(in_array($last_word, $tags_filter_by) ){ //$params[‘tag’] = ‘bread,baking’; posts with any of these tags $params[‘tag’] = trim(strtolower($last_word)); error_log(“filtering to only show posts … Read more

WordPress SQL snippets

A place to keep snippets of useful WordPress SQL. Do a select based on some meta field of a post ( i.e. search for a post that has a particular metafield  ) SELECT p.ID,p.post_title, MAX(CASE WHEN pm1.meta_key = ‘id_vim_node’ then pm1.meta_value ELSE NULL END) as field_i_want FROM wp_posts p LEFT JOIN wp_postmeta pm1 ON ( … Read more

How to add a form to a shortcode in WordPress (using PHP and Ajax)

Recently I needed to develop a Shortcode that would display a form, this blog post details what I did. I used Ajax ( which seems the best way to add forms to shortcodes, but I’d welcome any input from other developers on your approach). So without further ado lets get on with adding a form to … Read more