{"id":1997341,"date":"2021-03-23T11:02:52","date_gmt":"2021-03-23T11:02:52","guid":{"rendered":"https:\/\/toolset.com\/?post_type=course-lesson&#038;p=1997341"},"modified":"2021-03-25T06:27:08","modified_gmt":"2021-03-25T06:27:08","slug":"passing-arguments-to-views","status":"publish","type":"course-lesson","link":"https:\/\/toolset.com\/de\/course-lesson\/passing-arguments-to-views\/","title":{"rendered":"Passing Arguments to Views"},"content":{"rendered":"\n<p>Let\u2019s say you created a View that displays houses. In your site, houses have a custom checkbox field called &#8220;Available&#8221; that determines their availability on the market.<\/p>\n\n\n\n<p>You can create one View to display both available and &#8220;taken&#8221; houses simply by passing a URL argument to it.<\/p>\n\n\n\n<p>So, a user gets a list of &#8220;taken&#8221; houses when visiting the following URL:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>example.com\/houses_page\/?available=0<\/code><\/li><\/ul>\n\n\n\n<p>And they get a list of available houses when visiting this URL:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>example.com\/houses_page\/?available=1<\/code><\/li><\/ul>\n\n\n\n<p>As you can see, it&#8217;s the same page, only the &#8220;available&#8221; argument in the URL is different. This argument tells the View on that page how to filter the content.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to add filtering arguments to a View<\/h2>\n\n\n\n<p>You can use arguments to control a View output by:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Custom fields<\/li><li>Taxonomies<\/li><li>Post filters<\/li><li>Post relationships<\/li><\/ul>\n\n\n\n<p>To add an argument to a View you need to use a Query Filter. Select your <strong>View<\/strong> block in block navigation and in the right sidebar, expand the <strong>Content Selection<\/strong> section. Click <strong>Add a filter<\/strong> and choose the filter you want to use.&nbsp;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"d5903e0aaa62cf6a0f5094d69a0f152b\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/adding-a-query-filter-to-a-view.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/adding-a-query-filter-to-a-view-500x234.png\" alt=\"\" class=\"wp-image-1997489\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Adding a Query Filter to a View<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Filtering by custom fields<\/h2>\n\n\n\n<p>When you select a filter by custom fields the following dialog for selecting the filter&#8217;s options appears.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"7708f5e1ea0b0bceeb927ca180984853\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/custom-field-filter-url-parameter-gym-500x309.png\" alt=\"\" class=\"wp-image-1997479\"\/><div class=\"tb-image-caption\"><figcaption>Dialog for customizing a custom field filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>Use the first two dropdown menus to select the type of the data you want to compare (string,number, date, etc.) and the comparison function you need.&nbsp;<\/p>\n\n\n\n<p>In the third dropdown, select the <strong>URL parameter <\/strong>option and type in the parameter&#8217;s name you want to use. This means that the value is not hard-coded into the <strong>View<\/strong>, but will come from the page\u2019s URL.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Filtering by taxonomies<\/h2>\n\n\n\n<p>You can also filter a View by a specific taxonomy using arguments. When you add a filter by taxonomy, the following dialog appears.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"478b1de1d53e1d2e4b0305d94867a70f\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/Customising-a-Taxonomy-filter-500x312.png\" alt=\"\" class=\"wp-image-1997469\"\/><div class=\"tb-image-caption\"><figcaption>Customizing a Taxonomy filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>In our example, we selected the &#8220;Gym Types&#8221; taxonomy.<\/p>\n\n\n\n<p>Use the following steps to configure the URL parameter option:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>In the first dropdown, select the <strong>Set by one URL parameter <\/strong>option.<\/li><li>In the second dropdown, select if you want to compare your taxonomy&#8217;s slug or a name.<\/li><li>In the third dropdown, select how to include or exclude the compared values.<\/li><li>Finally, set the name of your URL parameter. You can use the suggested one or put in a custom one.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Filtering by Post filters<\/h2>\n\n\n\n<p>You can add different types of Post filters. For example, you can filter by an author, post ID, date, and more.&nbsp;<\/p>\n\n\n\n<p>If you select to filter by author, you will see this box appear:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"709837ab41857cd21ddf0b446d0bdc8d\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/customising-the-author-post-filter-500x395.png\" alt=\"\" class=\"wp-image-1997459\"\/><div class=\"tb-image-caption\"><figcaption>Customizing the author post filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>The <strong>URL parameter<\/strong> can be either the author <strong>ID<\/strong> or <strong>Username. <\/strong>In our example, we used the <strong>ID.<\/strong>&nbsp;<\/p>\n\n\n\n<p>The second input field lets you specify the name of the URL argument. This defaults to <strong>author-filter <\/strong>but you can customize it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Filtering by Post relationships<\/h2>\n\n\n\n<p>You can also pass arguments to a View based on <a href=\"https:\/\/toolset.com\/de\/course-chapter\/using-post-relationships-on-directory-sites\/\">post relationships<\/a>. You can find this option under <strong>Post filters<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"469d07e92bab248025b876ed8ce1f30f\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/toolset-passing-arguments-by-post-relationship-500x424.png\" alt=\"\" class=\"wp-image-1997449\"\/><div class=\"tb-image-caption\"><figcaption>Selecting the filter by post relationship<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>In the dialog that appears, select the post relationship you want to use. Then, set the option to check the post with an ID set by the URL parameter. If needed, you can also customize the name of this argument.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"6a40974de131d86a59abbe1c0d4c46ce\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/03\/post-relationship-url-parameter-500x391.png\" alt=\"\" class=\"wp-image-1997439\"\/><div class=\"tb-image-caption\"><figcaption>Filtering a View by a relationship filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p><\/p>\n","protected":false},"author":84277,"featured_media":1997891,"template":"","related-lesson":[8411],"class_list":["post-1997341","course-lesson","type-course-lesson","status-publish","has-post-thumbnail","hentry","related-lesson-view"],"_links":{"self":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1997341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson"}],"about":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/types\/course-lesson"}],"author":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/users\/84277"}],"version-history":[{"count":9,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1997341\/revisions"}],"predecessor-version":[{"id":2000207,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1997341\/revisions\/2000207"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media\/1997891"}],"wp:attachment":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media?parent=1997341"}],"wp:term":[{"taxonomy":"related-lesson","embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/related-lesson?post=1997341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}