ACF shortcode nested in component parameter string

Hi,

I pass the following parameter string to a component:
category_name={{dc:url:param key=“cat” fallback=“CAT_DEFAULT”}}&orderby={{dc:url:param key=“order” fallback=“rand”}}&posts_per_page=4

now I want to replace the string CAT_DEFAULT with an ACF field (set as selection, output as value).
This is displayed correctly as text via {{dc:acf:user_field field=“cat_default” user=“1”}}.

But this string doesn’t work as parameter for the component:
category_name={{dc:url:param key=“cat” fallback={{dc:acf:user_field field=“cat_default” user=“1”}}}}&orderby={{dc:url:param key="order " fallback=“rand”}}&posts_per_page=4

  1. How do I have to enter the complete string?

  2. If I enter the string as initial in the JSON script of the component: do all quotation marks have to be escaped with a leading backslash?

Hello Daryl,

Thanks for writing in!

You will have to escape the quotes:

category_name={{dc:url:param key="cat" fallback=\{\{dc:acf:user_field field="cat_default" user="1"\}\}}}&orderby={{dc:url:param key="order " fallback="rand"}}&posts_per_page=4

You can enclose the dynamic content with double quotes.

{
    "content"	: {
    	"type"		: 	"textarea",
    	"label"		:	"Content",
  		"initial"	:	 "{{dc:acf:user_field field='cat_default' user='1'}}"
	}
}

Hope this helps.

That doesn’t work yet.

You wrote “escape the QUOTES” but added leading backslashes before the BRACKETS { }? Or is something missing in the code sample above? This produces no output of the provider.

Could you please show the correct formatting of the complete string (not only the ACF field) as initial value in the JSON, it doesn’t work either:
{
“content” : {
“type” : “textarea”,
“label” : “Content”,
“initial” : ???
}
}

I added secure note, so you can have a direct look onto the fields.
I appreciate your help!

Hi Daryl,

As you are adding the Double Quotes inside another Double Quote you need to escape the inner one. If you use the Single Quote as shown in the reply of my colleague, you don’t need to escape.

Escaped Quote

category_name={{dc:url:param key=\"cat\" fallback={{dc:acf:user_field field=\"cat_default\" user=\"1\"}}}}&orderby={{dc:url:param key=\"order\" fallback=\"rand"}}&posts_per_page=4

Just add the above string into the Initial attribute and check if that works for you or not.

Thanks

Ruenels 1st answer throws parse errors:

And your 2nd answer doesn’t give an output.

I used single quotes now and checked it by putting the strings directly into the provider as query string.
This initial string is working fine:
category_name={{dc:url:param key=‘cat’ fallback=‘cat_default’}}&orderby={{dc:url:param key=‘order’ fallback=‘rand’}}&posts_per_page=4

How do I have to insert the ACF field as replacement of cat_default now?

This doesn’t work:
category_name={{dc:url:param key=‘cat’ fallback={{dc:acf:user_field field=\“cat_default\” user=\“1\”}}}}&orderby={{dc:url:param key=‘order’ fallback=‘rand’}}&posts_per_page=4

and this doesn’t work:
category_name={{dc:url:param key=‘cat’ fallback=’{{dc:acf:user_field field=‘cat_default’ user=‘1’}}’}}&orderby={{dc:url:param key=‘order’ fallback=‘rand’}}&posts_per_page=4

and this doesn’t work:
category_name={{dc:url:param key=‘cat’ fallback={{dc:acf:user_field field=‘cat_default’ user=‘1’}}}}&orderby={{dc:url:param key=‘order’ fallback=‘rand’}}&posts_per_page=4

Hi Daryl,

Have you copied the Query String from my reply, if not please try once by doing that? It seems that the Quote in your Query String is the reason behind your problem. It should be Single Quote open and close i.e. or Double Quote open and close i.e. ".
Not the Quote shown in your Query String.

“ ” or ‘ ’

Hope it helps.
Thanks

Hi Tristup,

maybe it has been confusing because I didn’t know how to paste the string as correct preformatted text here in the forum. Now I know.

I used exactly your correct copied string (added a second escape backslash at\"rand\", I think you missed that):

category_name={{dc:url:param key=\"cat\" fallback={{dc:acf:user_field field=\"cat_default\" user=\"1\"}}}}&orderby={{dc:url:param key=\"order\" fallback=\"rand\"}}&posts_per_page=4

It gives no output.

This is working fine:

category_name={{dc:url:param key='cat' fallback='cat_default'}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page=4

But replacing 'cat_default' by ’{{dc:acf:user_field field=‘cat_default’ user=‘1’}}’ (ACF field string with single quotes around) or {{dc:acf:user_field field=‘cat_default’ user=‘1’}} (no single quotes around) or {{dc:acf:user_field field=\"cat_default\" user=\"1\"}} (inner escaped double quotes) does not work.

Maybe it would be easier and time saving for us if you would login and try on your own.
I add secure note again.

Hello @Daryl_Shaw,

I am confused because you have this element parameter:

{
  "kategorie":{
    "label"	:	"Kategorie",
    "type": "text",
    "initial": "________________CORRECT FORMATTED STRING HERE PLEASE ____________________}"
  },
  "anzahl":{
    "label"	:	"Anzahl",
    "type": "text",
    "initial": "3"
  }
}

And then you have this Looper Provider:
category_name={{dc:url:param key='cat' fallback='GC-B-20'}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page=3

{{dc:url:param should be supplied URL parameters. This does not have anything to do with the element paramaters. If you are going to use “Kategorie” and “Anzahl” parameters, you will have this query string:
category_name={{dc:p:kategorie}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page={{dc:p:anzahl}}

And if you automatically load the default category from ACF field, you would something like this element parameters:

{
  "kategorie":{
    "label"	:	"Kategorie",
    "type": "text",
    "initial": "{{dc:acf:user_field field='cat_default' user='1'}}"
  },
  "anzahl":{
    "label"	:	"Anzahl",
    "type": "text",
    "initial": "3"
  }
}

Can you please point me to which page you are going to use this Component? I am trying to get the idea of how this component will display data.

Thank you.

Hi Ruenel, thanks for getting back to tihis!

I added Secure Note

Hello @Daryl_Shaw,

I still do not get the idea of what should be displayed by the Component. You can try this one:

{
  "kategorie":{
    "label"	:	"Kategorie",
    "type": "text",
    "initial": "{{dc:acf:user_field field='cat_default' user='1'}}"
  },
  "anzahl":{
    "label"	:	"Anzahl",
    "type": "text",
    "initial": "3"
  }
}

And then the query string might be something like:
category_name={{dc:url:param key='cat' fallback='{{dc:p:kategorie}}'}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page=posts_per_page={{dc:p:anzahl}}

From what I understand based on the query string, the “Working” page has a Looper that consists of a Component that will return post items that:

  • has a category being supplied by the “https://example.com/working?cat=name&order=ASC” url and;
  • when the category “name” does not exist, it falls back to the ACF user_field default category.

You have these choices for the default user category:

I am just not sure if the query string can return the correct post items because normally, the query string will use the category slug:

You can double-check the category parameters for the WordPress query here:

Best Regards.

Thanks for the further patient feedback.

Yes, exactly: The component is intended to represent a post listing in which tags in the URL can be used to set whether a specific category should be shown and how it should be sorted. If no category is defined in the URL, it should fall back to a default category to be set via the ACF field.

This query string with parameters results in the correct posts, regardless of whether I use GC-B-20 or gc-b-20 in text form directly as a fallback:

category_name={{dc:url:param key='cat' fallback='GC-B-20'}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page={{dc:param:anzahl}}

Now I just want to replace the fixed entry GC-B-20 with the parameter {{dc:param:kategorie}}. And its initial value should be given by the ACF field {{dc:acf:user_field field=‘cat_default’ user=‘1’}}. The query string you last formulated is in principle exactly what I want:

category_name={{dc:url:param key='cat' fallback='{{dc:param:kategorie}}'}}&orderby={{dc:url:param key='order' fallback='rand'}}&posts_per_page={{dc:param:anzahl}}

…and as initial parameter value for {{dc:param:kategorie}} then "initial": "{{dc:acf:user_field field='cat_default' user='1'}}"

But the query string will not yet produce an output, since the '{{dc:param:kategorie}}' does not seem to be accepted as a fallback. And neither if I set the initial paramater value of kategorie as:"initial": "{{dc:acf:user_field field='cat_default' user='1'}}" or directly as "GC-B-20" or "gc-b-20"

So “might be something like” remains my problem: It doesn’t work the way you proposed and I can’t find the right solution for integrating a parameter or the ACF field as fallback into the query string.

@ruenel: This topic isn’t closed yet, could you please come back to it?

To reduce it to the remaining problem:

The component parameter {{dc:param:fallback}} contains a text like GC-B-20

This query string works:
category_name={{dc:param:fallback}}

This query string also works:
category_name={{dc:url:param key='cat' fallback='GC-B-20'}}

But this query string doesn’t work:
category_name={{dc:url:param key='cat' fallback='{{dc:param:fallback}}'}}

So the query fails if I use {{dc:param:fallback}} instead of a fixed entry as fallback value.
How can I use {{dc:param:fallback}} as fallback entry so that it gives an output? (JSON syntax problem?)

Thanks for advice.

Unfortunately you can’t use dynamic content inside a dynamic content string like that. If you wanted to use dynamic content as a fallback like that, you’d probably setup provider that has a show condition and then another provider that uses the fallback when it’s the value is not set.

Unless I bandaid it which I’ll probably do just debating if this code is okay. This feature won’t be completed until we integrate a more advanced templater like twig or liquid, but it is something we want to do. The syntax would be a little different for that example in particular. I’ll keep this thread updated. It would look something like this.

{{dc:url:param key='cat' fallback='dc:param:fallback'}}

The syntax I wrote above will be supported for a fallback value in the next release. And in 6.2.0 beta2 most likely coming out friday.

Sounds great!
So I’ll wait for it and try…

Please give us some feedback thereafter.
Thank you.

@charlie: I really appreciate your quick integration of the dynamic fallback solution for this. Great!

It works as intended, and I think it’s a useful - and logical - extension of the query strings to be able to set “default” values taken from variables / ACFs before additional sorting or filtering via URL parameters.

I’m more of a designer than a coder, and it’s not always clear to me and tedious to create query strings combining different conditions and dynamic values. In this respect, the current syntax without two curly brackets for the fallback value is not entirely logical for me. You said: it’s a kind of bandaid solution, and after reading this long thread I’ll figure it out again for next projects :wink:

As debate contribution:
I really would appreciate a very simple “drag-drop/click system” where I could combine any values and ACFs and other fields with conditions to get more complex looper queries. Like the existing query builder but without limitations e.g. to combine includes and excludes of different post-categories and setting fallbacks for it - that could also contain of ACF values etc. It would be great to stay in cornerstone UI without having to understand JSON, Syntax, or to use PHP generations for queries…

Last question regarding this thread:

I use your new fallback option to “pre-filter” a blog in such a way that per default only those entries appear that contain a value from a component parameter {{dc:param:cat_filter}} as a category:
category_name={{dc:url:param key=‘cat’ fallback=‘dc:param:cat_filter’}}

What should the query string be if I want to invert that and only use posts as first fallback that exactly do NOT contain {{dc:param:cat_filter}} - but keep the possibility to filter via url param, too?
I was hoping for something like category_name={{dc:url:param key=‘cat’ fallback!=‘dc:param:cat_filter’}} (!= instead of =) but that doesn’t work…

Hello Daryl,

If you only want to return the post items that do not contain the {{dc:param:cat_filter}}, you will have to use a different query string. You can use this query string instead; category__in = array(1,2,3) or cat=-4 assuming that 4 is the category ID of the cat_filter and the rest of other category IDs are 1,2 and 3. Check out the category query for further explanation here:

Best Regards.

The challenge is, that I want to define the category via the ACF field dynamically.
I can’t use fixed category ID in the code, because it should be changeable via ACF.
Is there any possibility to get the category ID read out of the ACF field entry?
Let’s say, value of {{dc:param:cat_filter}}would be GC-B-20.
And then a query syntax like category_name={{dc:url:param key=‘cat’&category fallback=‘’}}&cat=-ID(dc:param:cat_filter) (?) that provides posts except those with GC-B-20 as category?