{"id":1698,"date":"2017-06-20T05:02:00","date_gmt":"2017-06-20T05:02:00","guid":{"rendered":"http:\/\/docs.contentviewspro.com\/?p=1698"},"modified":"2025-03-11T09:06:25","modified_gmt":"2025-03-11T09:06:25","password":"","slug":"set-dynamic-value-filtering-custom-field","status":"publish","type":"docs","link":"https:\/\/contentviewspro.com\/documentation\/article\/set-dynamic-value-filtering-custom-field\/","title":{"rendered":"Set dynamic value when filter by a custom field"},"content":{"rendered":"<p>With Content Views Pro, you can set a static value to filter content by custom field easily.<br \/>\nThis 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:<\/p>\n<ul>\n<li>Filter by Custom Field:<br \/>\n&#8220;Field Key&#8221;: select the custom field that you want to filter<br \/>\n&#8220;Value Type&#8221;: select Text (or another option depending on value type of selected field),<br \/>\n&#8220;Operator To Compare&#8221;: select <code>Equal (=)<\/code> (or another option depending on your needs),<br \/>\n&#8220;Value To Compare&#8221;: enter <code>1<\/code> (or enter actual value, please ensure value is <strong>not blank or empty<\/strong>).<br \/>\n<img decoding=\"async\" class=\"thumbnail\" style=\"max-width: 100%;\" src=\"https:\/\/contentviewspro.com\/documentation\/wp-content\/uploads\/2025\/03\/filter-ctf.png\" alt=\"\" \/><\/li>\n<li>then add this code to file <strong>functions.php<\/strong> of your active theme:\n<pre style=\"font-size: 13px; margin-top: 10px;\">\/\/ Content Views Pro - set value of custom field dynamically\nadd_filter( 'pt_cv_query_ctf_value', 'cvp_theme_query_ctf_dynamic_value', 100, 2 );\nfunction cvp_theme_query_ctf_dynamic_value( $args, $key ) {\n\tglobal $pt_cv_id;\n\tif ( $pt_cv_id === 'VIEW_ID' &amp;&amp; $key === 'NAME_OF_CUSTOM_FIELD' ) {\n\n\t\t\/* <span style=\"color: red;\">Todo: replace 1 by PHP expression\/function to set dynamic value for custom field<\/span> *\/\n\t\t$value = 1;\n\n\t\tif ( PT_CV_Functions::setting_value( PT_CV_PREFIX . 'enable-pagination' ) ) {\n\t\t\tglobal $pt_cv_id;\n\t\t\t$trans_key = 'cvp_ctf_value_' . $key . $pt_cv_id;\n\t\t\tif ( !defined( 'PT_CV_DOING_PAGINATION' ) ) {\n\t\t\t\tset_transient( $trans_key, $value, 1 * HOUR_IN_SECONDS );\n\t\t\t} else {\n\t\t\t\t$value = get_transient( $trans_key );\n\t\t\t}\n\t\t}\n\n\t\t\/\/ If value is not empty (0, null, false, empty string\/array)\n\t\tif ( !empty( $value ) ) {\n\t\t\t$args = $value;\n\t\t}\n\t}\n\n\treturn $args;\n}\n<\/pre>\n<p>and replace <strong>VIEW_ID<\/strong> with <a href=\"https:\/\/contentviewspro.com\/documentation\/article\/what-is-id-of-a-view\/\">ID of your View<\/a>, replace <strong>NAME_OF_CUSTOM_FIELD<\/strong> with name of the custom field.<\/li>\n<\/ul>\n<hr \/>\n<p>If you want to filter the posts that have custom field value is same as custom field value of current post, you can replace <code>$value = 1;<\/code> in above code with this code:<\/p>\n<pre style=\"font-size: 13px; margin-top: 10px;\">global $post;\n$meta = get_post_meta( $post-&gt;ID );\n$value = !empty( $meta[ 'NAME_OF_CUSTOM_FIELD' ][ 0 ] ) ? $meta[ 'NAME_OF_CUSTOM_FIELD' ][ 0 ] : 0;\n<\/pre>\n<p>(replace <strong>NAME_OF_CUSTOM_FIELD<\/strong> with name of the custom field)<\/p>\n<p><strong>Notice<\/strong>: This task requires coding skill. So please ensure that you know what to do.<\/p>\n<p>Best regards,<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: Filter by Custom Field: &#8220;Field Key&#8221;: select the custom field that you &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/contentviewspro.com\/documentation\/article\/set-dynamic-value-filtering-custom-field\/\"> <span class=\"screen-reader-text\">Set dynamic value when filter by a custom field<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"doc_category":[55,52],"doc_tag":[13,21],"class_list":["post-1698","docs","type-docs","status-publish","hentry","doc_category-advanced-modification","doc_category-custom-field","doc_tag-custom-field","doc_tag-custom-code"],"year_month":"2026-09","word_count":336,"total_views":"0","reactions":{"happy":"1","normal":"0","sad":"0"},"author_info":{"name":"admin1","author_nicename":"admin1","author_url":"https:\/\/contentviewspro.com\/documentation\/author\/admin1\/"},"doc_category_info":[{"term_name":"Advanced Modification","term_url":"https:\/\/contentviewspro.com\/documentation\/cats\/advanced-modification\/"},{"term_name":"Custom Field (core, ACF, Metabox, etc.)","term_url":"https:\/\/contentviewspro.com\/documentation\/cats\/custom-field\/"}],"doc_tag_info":[{"term_name":"custom field","term_url":"https:\/\/contentviewspro.com\/documentation\/tags\/custom-field\/"},{"term_name":"custom PHP code","term_url":"https:\/\/contentviewspro.com\/documentation\/tags\/custom-code\/"}],"_links":{"self":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1698","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/comments?post=1698"}],"version-history":[{"count":10,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1698\/revisions"}],"predecessor-version":[{"id":6051,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/docs\/1698\/revisions\/6051"}],"wp:attachment":[{"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/media?parent=1698"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_category?post=1698"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/contentviewspro.com\/documentation\/wp-json\/wp\/v2\/doc_tag?post=1698"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}