Title: Shortcode with Where clause default value
Last modified: July 13, 2020

---

# Shortcode with Where clause default value

 *  Resolved [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/)
 * I am currently developing a site using PODS and I have created a page which I’m
   adding a list of items in. These POD items have a relationship field in and I’d
   like to filter the list down based on this field and a querystring value.
 * I have have made the required updates to wp-config and I can see the filtering
   working, however I’d like the initial list to be unfiltered as it is blank at
   thge moment when no querystring value is used. Is this possible within a shortcode
   or do I have to create a custom PHP page to do this with queries?
 * My shortcode is:
 * [pods name=”primary-workshop” template=”Primary Workshops” where=”workshop_category.
   meta_value = ‘{@get.STEMCat}'”]
 * Thanks in advance.
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fshortcode-with-where-clause-default-value%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 12 replies - 1 through 12 (of 12 total)

 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13120826)
 * Hi [@lbtdev](https://wordpress.org/support/users/lbtdev/)
 * Sorry, the `where` param cannot be wrapped in any form of “if” logic.
    You’ll
   have to go with PHP templates in order to do this.
 * Cheers, Jory
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13122070)
 * Hi Jory. Thanks for thre quick response. I figured this would be the case, but
   thought I’d check as the same concept in something like .net where the where 
   clause would read …meta_value = ” would yield all results instead of no results.
 * Anyway
 * I have now created a page template for this and it’s running fine, until I add
   the where clause and then I get a SQL syntax error. If I include t. before the
   field name then I get an unknown column error, so I think the column name is 
   correct and as far as I can see everything else should be fine. I have tried 
   swicthing the ” and ‘ around as well as using ` but nothing seems to work.
 *  My code is below:
 *     ```
           $var = "S"; // Placeholder for the moment. Replace with querystring val when working
           $params = array(        
       		'limit'   => -1, 
       		'where'   => 'workshop_category.meta_value = "' . $var . '"' //Works with this line taken out
       	);
   
           $PWpod = pods('primary-workshop',$params);
   
           if ( $PWpod->total() > 0 ) {...
       ```
   
 * This works when I take out the where clause (S is a known valid value). I’ve 
   looked through several posts and tried several different itterations of this 
   including with and with out the ‘t.’ and using LIKE but I still get the same 
   error.
 * My shortcode this is dervied from is: `[pods name=”primary-workshop” template
   =”Primary Workshops” where=”workshop_category.meta_value = ‘{@get.STEMCat}'”]`
 * Please advise.
 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13122575)
 * Hello [@lbtdev](https://wordpress.org/support/users/lbtdev/)
 * Just to be sure, since you named the field `workshop_category`. Is it an actual
   category (taxonomy) or a plain meta value?
 * Cheers, Jory
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13123767)
 * It’s a plain meta value. Setup as a field in the POD:
    Field Type: Relationship
   Related To: Simple Custom Defined Types: S|Science T|Technology E|Engineering
   M|Maths
 * It’s a singular dropdown list and I am using the value to evaluate against the
   querystring (?STEMCat=x)
 * if it helps, my updated code is:
 *     ```
       	$selectedCat = $_GET["STEMCat"];
   
       	if($selectedCat){
       		$params = array(        
       			'where'   => "workshop_category.meta_value = '" . $selectedCat . "'",
       			'limit'   => -1, 
       		);
       	} else {
       		$params = array(        
       			'limit'   => -1, 
       		);
       	}
   
           $PWpod = pods('primary-workshop')->find($params);
       ```
   
 * Thanks in advance
 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13125384)
 * Hi [@lbtdev](https://wordpress.org/support/users/lbtdev/)
    I might be missing
   something but I’d say that should work. Could you check the SQL by adding ?pods_debug_sql
   =1 to your URL. Cheers, Jory
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13125454)
 * [@keraweb](https://wordpress.org/support/users/keraweb/) thank you. Yes, I can’t
   see any issue with it and I’ve tried several different itterations with no success.
 * The debug trace came back with:
 * Database Error; SQL: SELECT DISTINCT `t`.* FROM `vrw_posts` AS `t` LEFT JOIN `
   vrw_postmeta` AS `workshop_category` ON `workshop_category`.`meta_key` = ‘workshop_category’
   AND `workshop_category`.`post_id` = `t`.`ID` WHERE ( ( `workshop_category`.`meta_value`
   = ‘E’ ) AND ( `t`.`post_type` = “primary-workshop” ) AND ( `t`.`post_status` 
   IN ( “publish” ) ) ) ORDER BY `t`.`menu_order`, `t`.`post_title`, `t`.`post_date`;
   Response: You have an error in your SQL syntax; check the manual that corresponds
   to your MySQL server version for the right syntax to use near ‘ = ‘E’ ) AND (`
   t`.`post_type` = “primary-workshop” ) AND ( `t`.`post_status` I’ at line 11
 * Adding t. before workshop_category give me an unknown coulmn error. So it looks
   as though I’m looking in the right place. I have tried escaping the string via\
   as well as using the PHP functions to escape the strings, I have also tried string
   interpolation but always get this syntax error
    -  This reply was modified 5 years, 11 months ago by [Tony Law](https://wordpress.org/support/users/lbtdev/).
 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13125754)
 * Hi [@lbtdev](https://wordpress.org/support/users/lbtdev/)
    Correct, the workshop_category
   shouldn’t be prefixed. I tested this SQL locally without errors so I think there
   must be some wrong characters within your SQL `where` statement. Are you sure
   you are using the correct quotes? (WP forum converts these characters so I can’t
   see). For strings these need to be simple single quotes. Cheers, Jory
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13125847)
 * [@keraweb](https://wordpress.org/support/users/keraweb/) Thanks for your help.
 * I have run this directly against the SQL database and hacked around with the 
   query. Turns out that there are spaces in the built query which my DB version
   didn’t like. Removing these fixed the issue!
 * I amended the following params to remove the spaces between and it now works.
 *     ```
       	if($selectedCat){
       		$params = array(        
       			'where' => 'workshop_category.meta_value="' . $_GET["STEMCat"] . '"',
       			'limit' => -1, 
       		);
       	} else {
       		$params = array(        
       			'limit' => -1, 
       		);
       	}
       ```
   
 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13126149)
 * Hi [@lbtdev](https://wordpress.org/support/users/lbtdev/)
 * Ah ok! Weird issue, just curious, what type of SQL database are you running and
   what version?
 * Cheers, Jory
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13126424)
 * As far as I know it’s the latest version of MySql.
 *  Plugin Author [Jory Hogeveen](https://wordpress.org/support/users/keraweb/)
 * (@keraweb)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13126763)
 * You can see the SQL info in Pods > Settings to be sure!
    I find it such a weird
   issue that I’d like to verify and reproduce locally.
 *  Thread Starter [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * (@lbtdev)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13130459)
 * [@keraweb](https://wordpress.org/support/users/keraweb/) No problem. Yes it does
   seem very strange!
 * MySQL Version: 5.7.25

Viewing 12 replies - 1 through 12 (of 12 total)

The topic ‘Shortcode with Where clause default value’ is closed to new replies.

 * ![](https://ps.w.org/pods/assets/icon.svg?rev=3286397)
 * [Pods - Custom Content Types and Fields](https://wordpress.org/plugins/pods/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/pods/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/pods/)
 * [Active Topics](https://wordpress.org/support/plugin/pods/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/pods/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/pods/reviews/)

 * 12 replies
 * 2 participants
 * Last reply from: [Tony Law](https://wordpress.org/support/users/lbtdev/)
 * Last activity: [5 years, 10 months ago](https://wordpress.org/support/topic/shortcode-with-where-clause-default-value/#post-13130459)
 * Status: resolved