Overriding Parameters For An Easy Pods Query

Easy Pods Banner

While the Caldera Easy Pods editor gives you an easy way to visually build Pods queries, which in most cases is all you need, there are options to override the saved settings before running the query. The is is useful when you want to override a setting based on the value of a post or user meta field, or create a variation on a query.

Modifying Query Parameters With The Shortcode Or Function

Caldera Easy Pods queries can be outputted either with the shortcode easy_pod or the function cep_render_easy_pod(). Both of these methods only requires one parameter, the name of the Easy Pod. But, they do accept to optional parameters, “params” and “template”.

The params argument, can be an array with any valid set of parameters accepted by Pods::find(). This parameter is of limited use when using the easy_pods shortcode in the WordPress post editor. That limitation is addressed in the next section. On the other hand, it is useful, when using the function.

For example, when used in a theme template, it could be used to optionally modify a query, based on the value of a user meta field:

<?php
    //find a user's favorite genre if they are logged in.
    $favorite = false;
    if ( is_user_logged_in() ) {
        $favorite = get_user_meta( get_current_user_id(), 'favorite_genre', true );
    }
    
    //set params if $favorite is a string, if not, set them to null.
    if ( is_string( $favorite ) ) {
        $params[ 'genre.name' ] = $favorite;
    }else{
        $params = null;
    }
    
    echo cep_render_easy_pod( 'books', $params );

The “template” parameter is used for changing which Pods Template is used for rending the output of a Caldera Easy Pods query.

Using A Filter To Change Parameters

For the Caldera Answers Search we use a shortcode inside of a page to output the search form that we built with Caldera Easy Pods. In order to randomize the order of the results, we use the “caldera_easy_pods_query_params” filter. The callback for this filter checks that the slug of the Easy Pod corresponds to the query we are looking to modify, if so, it adds an orderby argument to randomize the order:

<?php
   add_filter( 'caldera_easy_pods_query_params', function( $params, $pod, $tags, $easy_pod_slug ) {
      if ( 'caldera_answers' == $easy_pod_slug ) {
         $params[ 'orderby' ] = 'rand()';
      }
   
      return $params;
   
   }, 10, 4);
   

This is necessary because we can not pass an array of arguments into the shortcode from the WordPress post editor. Even if we could, the SQL function rand() being used would be stripped for security reasons.