Posts

Showing posts from September, 2011

Passing a query string to your WordPress site and show something useful

Objective: Say my website runs on Word Press (henceforth known as WP) at http://www.abcdxyz.com A post might look like the following http://www.abcdxyz.com/?page_id=21 And the post gets rendered nicely. Say I want to pass a Query String (henceforth known as QS) test=1, based on which I want to show a plain text to user or in other words simple want to hook on to WP rendering technique and modify it. My intended URL becomes http://www.abcdxyz.com/?test=21 Normally if you call this, WP would render the default page to you and simply dispose off your idea of passing and doing something useful with QS. Solution: We have to modify the $query_vars, a mechanism of WP internals, with hook. 1. In your plug in file write the following hooks add_filter('rewrite_rules_array', 'add_new_allowed_qs'); You may define the body of the function as follows: function add_new_allowed_qs( $query_vars ){ array_push($query_vars, 'test'); // return return $q

Left join should fetch data from left table even when right table doesn't have a reference

Okay first things first, the title may be confusing. Objective: There are two tables, Parent and Children. I'm creating a listing page where I'm fetching the parents and the children count, yes, please note it's count. Structures: Parent: id, parent_name Children: id, parent_id, child_name A single sql is needed which would fetch me the following columns: Parent.id, Parent.parent_name, count(child_name) as no_of_children Say for parent Tomtom there's two children, For Kawasaki there's 3 and for Mickey there's none (no association in Children table, no records absolutely). I want a list like the following: Row 1: 1 Tomtom 2 Row 2: 2 Kawasaki 1 Row 4: 3 Mickey 0 I tried the following SQL: select a.id as parent_id, a.parent_name, count(b.id) as no_of_children from Parent a LEFT JOIN Children b ON a.id = b.parent_id group by b.parent_id The output: Only the record mappings where at least 1 association is present in the Childr