How to set dynamic value when filter content by a custom field?

With Content Views Pro, you can set a static value to filter content by custom field easily.
This document will help you to set dynamic value (from URL parameters, current author id, and so on) to filter by custom field. Here is how:

  • please ensure that you set a value for the custom field.
    Any non-empty value is ok, for example: a number, a character.
    Just don’t leave it blank or empty, otherwise the code in next step will NOT be processed.

    CVP - set dynamic value of custom field

  • then add this code to file functions.php of your active theme:
    // Content Views Pro - set value of custom field dynamically
    add_filter( 'pt_cv_query_ctf_value', 'cvp_theme_query_ctf_dynamic_value', 100, 2 );
    function cvp_theme_query_ctf_dynamic_value( $args, $key ) {
    	if ( $key === 'NAME_OF_CUSTOM_FIELD' ) {
    
    		$value = 1;
    		// Replace 1 by the dynamic value, for example:
    		// $value = esc_sql( $_GET[ 'parameter_1' ] );
    
    		if ( PT_CV_Functions::setting_value( PT_CV_PREFIX . 'enable-pagination' ) ) {
    			global $pt_cv_id;
    			$trans_key = 'cvp_ctf_value_' . $key . $pt_cv_id;
    			if ( !defined( 'PT_CV_DOING_PAGINATION' ) ) {
    				set_transient( $trans_key, $value, 1 * HOUR_IN_SECONDS );
    			} else {
    				$value = get_transient( $trans_key );
    			}
    		}
    
    		$args = $value;
    	}
    
    	return $args;
    }
    

    and replace NAME_OF_CUSTOM_FIELD with name of the custom field. Also supply the code to set dynamic value for custom field.

Notice: This task requires coding skill. So please ensure that you know what to do.

Best regards,