Skip to content

Instantly share code, notes, and snippets.

@vfontjr
Last active April 21, 2022 12:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vfontjr/c9e672e52f8ca31aee651ae5509eb4c8 to your computer and use it in GitHub Desktop.
Save vfontjr/c9e672e52f8ca31aee651ae5509eb4c8 to your computer and use it in GitHub Desktop.
Formdiable Masterminds - count entries shortcode for https://formidable-masterminds.com/count-entries-shortcode/
<?php
add_shortcode( 'count_entries', 'fm_count_entry_records' );
function fm_count_entry_records( $atts ) {
/* define/use variables to access WordPress database functions
* and Formidable's tables
*/
global $wpdb;
$prefix = $wpdb->prefix;
$frm_items = $prefix . 'frm_items';
$frm_item_metas = $prefix . 'frm_item_metas';
/* this is the default attribute structure for receiving the shortcode parameters
* this structure should not modified. This is what intgrates the
* API with any Formidable functions or custom code
* written below
*/
$atts = shortcode_atts( array(
'field_id' => '0',
'field_key' => '',
'field_value' => '',
), $atts );
/* is the shortcode using the field_id or field_key as a parameter */
if ( !empty( $atts['field_key'] ) ) {
/* field_key is the parameter, retrieve the field's ID with a Formidable function */
$field_id = FrmField::get_id_by_key( $atts['field_key'] );
} else {
$field_id = $atts['field_id'];
}
/* assign the search value to a variable used in the custom SQL */
$meta_value = $atts['field_value'];
/* build the custom SQL */
$sql = $wpdb->prepare("SELECT count(*) FROM `{$frm_item_metas}` WHERE field_id = %d and meta_value = %s", $field_id, $meta_value);
/* execute the custom SQL */
$record_count = $wpdb->get_var( $sql );
/* return the result to display in place of the shortcode in your content */
return $record_count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment