{"id":947,"date":"2025-03-24T14:28:06","date_gmt":"2025-03-24T14:28:06","guid":{"rendered":"https:\/\/wpgeodirectory.com\/documentation\/article\/add-custom-conditions-to-dynamic-content-widget\/"},"modified":"2025-06-02T13:06:35","modified_gmt":"2025-06-02T12:06:35","slug":"add-custom-conditions-to-dynamic-content-widget","status":"publish","type":"gd_place","link":"https:\/\/wpgeodirectory.com\/documentation\/article\/code-snippets\/add-custom-conditions-to-dynamic-content-widget\/","title":{"rendered":"Add Custom Conditions to Dynamic Content Widget"},"content":{"rendered":"\n<p>When working with <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/core-widgets\/gd-dynamic-content\/\">GD Dynamic Content<\/a> and <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/core-widgets\/gd-post-badge\/\">GD Post Badge<\/a> widgets, you can use the following code snippet to add custom conditions such as <strong>is_contains_any <\/strong>and <strong>is_not_contains_any <\/strong>to your shortcodes.&nbsp;<\/p>\n\n\n\n<p>Here is an example of such usage:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#091;gd_dynamic_content key=&quot;package_id&quot; condition=&quot;is_contains_any&quot; search=&quot;3,11,15&quot;&#093;DO SOMETHING&#091;\/gd_dynamic_content&#093;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><br>Adding Custom Conditions<\/h2>\n\n\n\n<div class=\"wp-block-blockstrap-blockstrap-widget-alert d-flex align-items-center fade show alert alert-info mb-3\" role=\"alert\"><span class=\"fas fa-info-circle me-2\"><\/span><span class=\"flex-grow-1\">We recommend using the <a href=\"https:\/\/wpgeodirectory.com\/documentation\/article\/useful-plugins\/code-snippets-plugin\/\">Code Snippets<\/a> plugin to add snippets to your site.<\/span><\/div>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">function gd_snippet_badge_conditions( $conditions ) {\n\t$conditions&#091;&#039;is_contains_any&#039;&#093; = __( &#039;is contains any of&#039;, &#039;geodirectory&#039; );\n\t$conditions&#091;&#039;is_not_contains_any&#039;&#093; = __( &#039;is not contains any of&#039;, &#039;geodirectory&#039; );\n\treturn $conditions;\n}\n\nadd_filter( &#039;geodir_badge_conditions&#039;, &#039;gd_snippet_badge_conditions&#039;, 10, 1 );\n\nfunction gd_snippet_dynamic_content_check_match_found( $match_found, $args, $find_post ) {\n\tif ( ! empty( $args&#091;&#039;condition&#039;&#093; ) &amp;&amp; in_array( $args&#091;&#039;condition&#039;&#093;, array( &#039;is_contains_any&#039;,  &#039;is_not_contains_any&#039; ) ) ) {\n\t\t$match_found = false;\n\t\t$match_field = $args&#091;&#039;key&#039;&#093;;\n\t\tif ( isset( $find_post-&gt;{$match_field} ) &amp;&amp; trim( $find_post-&gt;{$match_field} ) !== &#039;&#039; ) {\n\t\t\t$match_value = geodir_strtolower( stripslashes( $find_post-&gt;{$match_field} ) );\n\t\t\t$search_value = geodir_strtolower( stripslashes( $args&#091;&#039;search&#039;&#093; ) );\n\t\t\t$match_value = explode( &quot;,&quot;, $match_value );\n\t\t\t$search_value = explode( &quot;,&quot;, $search_value );\n\t\t\tforeach ( $search_value as $value ) {\n\t\t\t\t$value = trim( $value );\n\t\t\t\tif ( $value !== &#039;&#039; &amp;&amp; in_array( $value, $match_value ) ) {\n\t\t\t\t$match_found = true;\n\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\tif ( $args&#091;&#039;condition&#039;&#093; == &#039;is_not_contains_any&#039; ) {\n\t\t\t$match_found = $match_found ? false : true;\n\t\t\t}\n\t\t}\n\t}\nreturn $match_found;\n}\n\nadd_filter( &#039;geodir_dynamic_content_check_match_found&#039;, &#039;gd_snippet_dynamic_content_check_match_found&#039;, 10, 3 );\n<\/code><\/pre>\n\n\n\n<p>The above snippet will work with the GD &gt; Dynamic Content widget. If you wish to use it with the GD &gt; Post Badge widget or shortcode, simply add an extra filter:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-php\">add_filter(&#039;geodir_post_badge_check_match_found&#039;, &#039;gd_snippet_dynamic_content_check_match_found&#039;, 10, 3 );<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with GD Dynamic Content and GD Post Badge widgets, you can use the following code snippet to add custom conditions such as is_contains_any and is_not_contains_any to your shortcodes.&nbsp; Here is an example of such usage:&nbsp; Adding Custom Conditions The above snippet will work with the GD &gt; Dynamic Content widget. If you wish [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"article\/tags":[610,807,808,809,810],"article\/categories":[211,433],"class_list":["post-947","gd_place","type-gd_place","status-publish","hentry","gd_place_tags-widgets","gd_place_tags-custom-conditions","gd_place_tags-dynamic-content","gd_place_tags-post-badge","gd_place_tags-shortcodes","gd_placecategory-tips-tricks-more","gd_placecategory-code-snippets"],"_links":{"self":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article"}],"about":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/types\/gd_place"}],"author":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/comments?post=947"}],"version-history":[{"count":1,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/947\/revisions"}],"predecessor-version":[{"id":1971,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/947\/revisions\/1971"}],"wp:attachment":[{"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/media?parent=947"}],"wp:term":[{"taxonomy":"gd_place_tags","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/tags?post=947"},{"taxonomy":"gd_placecategory","embeddable":true,"href":"https:\/\/wpgeodirectory.com\/documentation\/wp-json\/wp\/v2\/article\/categories?post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}