Skip Navigation

[Resolved] CRED not getting postID in generic field, default value with view shortcode

This support ticket is created 7 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 13 replies, has 3 voices.

Last updated by Beda 7 years, 3 months ago.

Assisted by: Beda.

Author
Posts
#548019

I have a page
This page has a view
The view is limited to return 1 post
The view is filtered with url param post id
Inside view loop I have the shortcode of a CRED new post form
Inside the CRED form I have another view that I want to filter with a shortcode for current post-id
The current post-id does not work

The shortcode returns VARIOUS results:

This returns NO posts:

  [cred_generic_field field='choose-date' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="select-bookings-datum-raw" wpvprchildof="[wpv-post-id]"] ]
}
[/cred_generic_field]

This returns ALL posts:

[credform class='cred-form cred-keep-original']
[cred_field field='form_messages' value='']
 [wpv-view name="select-bookings-datum-raw" wpvprchildof="[wpv-post-id]"] ]
[/credform]

Outside of the form but still in the loop - outputs CORRECT result

	<!-- wpv-loop-start -->
		<wpv-loop>
          <h3>[wpv-post-title]</h3>
          [wpv-view name="select-bookings-datum-raw"  wpvprchildof="[wpv-post-id]"]]
          [cred_form form='ny-bokning' form_name='Ny Bokning']
		</wpv-loop>
	<!-- wpv-loop-end -->
#548022

Additional info
If i don't filter the view with with wpvprchildof="[wpv-post-id]" but instead filter on the current post in the loop all is working.
But this is not an option for me because I use the same form on many places. Sometimes it is on the single-post layout and other times inside a view loop like this case.

If I could set the filter to Child of the current post in the loop OR child of post where this view is shown - but this is not available as a setting.

#548045

Hi Tina,
Inside the CRED form I have another view that I want to filter with a shortcode for current post-id
The current post-id does not work

When you say current post ID, do you mean the ID of the current post in the Loop, i.e. the post ID from the URL parameter? Or do you mean the ID of the page where this View is placed?

#549967

The cred form is inside a view loop.
The view is filtered with url param (postid) returning only one post.

Inside the cred form I have another view that returns all posts (I use it to populate a select)
I tried to filter this view with:
Child of the current post in the loop
Url parameter
Shortcode parameter

None of the above returns the view result.
If I place the view outside, of the generic select, directly in form, I do get the desired results

#550092

Okay let's try to narrow down the issue. Test the generic field using a hard-coded post ID:

[cred_generic_field field='choose-date' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="select-bookings-datum-raw" wpvprchildof="12345"] ]
}
[/cred_generic_field]

Modify 12345 to be a known parent post ID with multiple children. If the generic field options are populated, then we know for sure that your View is returning the proper raw text. We can eliminate that as the source of the problem and continue to the next step. If not, we know we need to investigate the View output.

Next test - Instead of using [wpv-post-id] to determine the parent ID, what if you use a custom shortcode that directly accesses the URL parameter?

add_shortcode( 'wpv-post-param', 'wpv_post_param_shortcode' );

function wpv_post_param_shortcode( $atts ) {
  if ( !empty( $atts['var'] ) ) {
    $var = (array)$_GET[$atts['var']];
    return esc_html( implode( ', ', $var ) );
  }
}

Then replace wpv-post-id in your CRED generic field shortcode:

[cred_generic_field field='choose-date' type='select' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":[],
"options":[ [wpv-view name="select-bookings-datum-raw" wpvprchildof="[wpv-post-param var='postID']"] ]
}
[/cred_generic_field]

Replace 'postID' with the URL parameter you are using to filter the parent View. This bypasses any confusing [wpv-post-id] shortcodes that might be operating in the wrong context.

#550329

1.
Your test number one worked.
Hard-coded post-id returned desired result.

2. my own post id shortcode
I did an additional test to see if get_the_ID() works at all
I created this short code as substitute for wpw-post-id
And it has the same result. If I add the shortcode - like so: [my_own_postid] - directly to cred "body" it outputs the correct current post-id, but when inside the generic field shortcode, I get nothing.

function my_own_postid_function( $atts ) {
  $postid = get_the_ID();
    return $postid;
}
add_shortcode( 'my_own_postid', 'my_own_postid_function' );

3.
Your third test generated the same result. Inside the generic field shortcode it doesn't output anything, but placed directly in form "body" it outputs what it should.

4.
BUT!!!
Something extremely strange happened.
IF I place your ,
[wpv-view name="select-bookings-datum-raw" wpvprchildof="[wpv-post-param var='postID'])
directly in CRED form body ,
AND in the generic field shortcode – both views are outputted correctly!?
IF I remove the shortcode from CRED body, the view (inside generic field) doesn't output any more.

5.
I did the same test with [my_own_postid]
[wpv-view name="select-bookings-datum-raw" wpvprchildof="[my_own_postid]"]
placed it both in form-body and inside the generic field.
This time it outputs all posts (in form-body) ignoring the views filter settings, and inside the generic field - nothing.
(The same result as when using wpv-post-id

#550377

AND in the generic field shortcode – both views are outputted correctly!?
Okay, that is interesting. Add the wpv-post-param shortcode in Toolset > Settings > Front end Content > Third-party shortcode arguments, then remove the View shortcode that was placed directly in the CRED form body. Keep the View shortcode in the generic field options, and let me know the results.

#550423

I added the shortcode to views settings. And removed it from from-body, kept it only in the generic field.
I get nothing again.

So I put in form body again and this time I did not get the desired result I got all posts.
Just like when I use my_own_postid or wpv-post-id

So these tests are inconsistent.

I am on the beta versions, can this have anything to do with the various results?

#550425

I tried to go back to original code
[wpv-view name="select-bookings-datum-raw" wpvprchildof="[wpv-post-id]"]

and put this inside the generic AND in form body and I got the filtered view as I wanted. But If I remove it from form body I don't get anything.

But this is inconsistent, sometimes I get all posts ... and sometimes the filtered view. But always nothing If I don't keep the code in form body.

#550427

Juan currently has full access (including ftp-account) to this staging.site
url to form: hidden link

url to page with problem (with sample postid-param) - you must be logged in as admin to use the form
hidden link

DO NOT USE THIS FORM with ANY OTHER POST ID !!!!!!!! - it will send notifications to users ...
Or at least don't submit the form with any other post-id, you can always load the page πŸ™‚

#550508

OK thanks, I've asked Juan for the login credentials. I'll open the private reply fields here, since Juan is already signed off for the day. If you care to share the credentials here, that's fine, otherwise I will wait for Juan to respond.

Regarding the beta versions, it's always a possibility they are causing a problem, but I'd rather not speculate until I can analyze your site setup and run some tests locally with both the current and beta plugins.

#551038

Hi, after running several tests on my own, I can replicate this issue consistently with only Toolset plugins and a default theme. This occurs with the current releases, so I do not think it's related to the betas. The only workaround I was able to find was as you have described - place the View in the CRED body before the generic field. You can wrap it in a hidden div for now, at least to continue development while we look into the issue further. I've escalated this concern to our 2nd tier support team to get some additional assistance. I'll update you as soon as I hear back from them.

#551739

Hi, unfortunately the feedback I've received is that using Views to generate the options for a generic fields is not a supported use of Views. Views are designed to render data, not generate data, and issues arising from using Views as a data provider are not considered bugs. Our 2nd tier support team will pick up this ticket shortly and follow up with you.

#552397

Hey, Tina.

As Christian already pointed out, I have to be firm on this, and there is not much we can discuss.

Views has been "misused" a lot in the past, mainly for exactly what you were doing here:
Use it as a Data Provider.

And that has been declared a not supported and not intended method to use Views.
We provided a small snippet to make it possible again after we introduced some default HTML around the Loop that broke all such "data providing setups."
But we also clearly stated that this is under own responsibility to be used and that all of the Views setting such as parametric search and AJAX calls might and most likely will, break.

We will not debug issues surging from the usage of that Custom Code.

What I suggest is to use PHP for this, create your ShortCode that returns the valid JSON you need, or single Post ID, if you need only on value.

It is not to recommend anyway to use Views to return just one post. It's usually a bit overkill to use Views for such purposes.

Also, Views ShortCodes in a Generic (or any other) ShortCode attribute to provide the data to it, is not supported, and it does not work since now more than a Year, without custom code.
WIth Custom Code, you can filter that View Loop to return a clean result, but as said, this is the instructions we have been passing along to the Users when we provided the Customs Code example:

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'prefix_clean_view_output', 5, 2 );

function prefix_clean_view_output( $out, $id ) {
if ( $id == '375' ) {
$start = strpos( $out, '<!-- wpv-loop-start -->' );
if ( 
$start !== false
&& strrpos( $out, '<!-- wpv-loop-end -->', $start ) !== false
) {
$start = $start + strlen( '<!-- wpv-loop-start -->' );
$out = substr( $out , $start );
$end = strrpos( $out, '<!-- wpv-loop-end -->' );
$out = substr( $out, 0, $end );
}
}
return $out;
}

1. Step by step:

- It hooks early in the View output.
- Priority 5 is mandatory as we already do some other cleaning at priority 10.
- It only affects a View with an ID of 375, adjust acordingly if needed.
- It only affects Views with actual results: if the View matches no result, the same β€œNo items found” or whatever you have between the wpv-no-items-found shortcode applies.
- It returns only what is between the HTML comments <!– wpv-loop-start –> and <!– wpv-loop-end –> , excluding them. Only content between those HTML comments is returned, as is.

2. Notice that with this applied to a given View ID:
- Pagination, especially AJAX pagination, will not work.
- Parametric search, specially AJAXed parametric search, will not work.
- Other future features will not work either.

So this is to be used if and only if you know what you are doing and you are going to use the View output as source for anything else.

But also notice that this only clears the View structure.

Building as example JSON inside a View output is wrong because we can not address quoting problems.

Please use this with caution, note the side effects and use it responsibly.

If you have found this code snippet anywhere on this forum without any of the above warnings, please pass the links to me as I would have to take internal measures

But anyway, I noticed you try to add a Post ID to the Generic Field with this:

function my_own_postid_function( $atts ) {
  $postid = get_the_ID();
    return $postid;
}
add_shortcode( 'my_own_postid', 'my_own_postid_function' );

I suggest to read this shortly:
https://wordpress.stackexchange.com/questions/112398/what-is-the-difference-between-post-id-and-get-the-id

Note the difference of $post->ID and get_the_ID().

This ShortCode works perfectly fine on the stable releases.
It also works on a View, where I call a CRED form and hence, have a "dynamic" set of Posts.

So, conclusion:

1. Using Views ShortCodes in that Generic Field is possible, but wil have sideeffects, and we will not assist such custom code

2. The Custom ShortCode to me seems to work just fine on the stable releases.

3. If you have issues populating a CRED Field with a Custom ShortCode value, we can assist this limitedly, as example by cehcking it the outout is correct and what it could not appear in the form

Thank you for understanding.