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.