Skip Navigation

[Gelöst] Views filter for JSON-output for CRED generic field

This support ticket is created vor 7 Jahre, 6 Monate. 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 5 Antworten, has 2 Stimmen.

Last updated by dianaM-4 vor 7 Jahre, 6 Monate.

Assisted by: Beda.

Author
Artikel
#436797
FilterEmptyField.JPG

I am population a CREDs-checkbox-field with data from another custom-post-type by a generic-field and JSON-output of a view. With your help from my other thread https://toolset.com/forums/topic/populate-a-cpt-field-in-cred-with-drop-down-options-from-another-cpt/ I got this to work properly.

But now I need to filter the view (with JSON-output for the generic field). I tried with conditional-if like this:

[wpv-conditional if="( $(wpcf-akt-bis) gte [my-today] ) OR ( $(wpcf-akt-bis) eq '' "]
          [wpv-item index=1]{"value":"[wpv-post-title format="meta" meta="ID"]","label":"[wpv-post-title format="meta" meta="nickname"]"}
          [wpv-item index=other],{"value":"[wpv-post-title format="meta" meta="ID"]","label":"[wpv-post-title format="meta" meta="nickname"]"}
          [/wpv-conditional]

But then there is no output in my cred-form. So I guess, wpv-conditional doesn't work inside the loop, when filling in the results as a generic-field in CRED?

I then tried to filter the view with the views filter-options. This works for my first filter (for dates equal or after TODAY). But I can't figure out, how I can filter for empty fields there (see my screenshot). The filter should show all of my posts with empty date-field, too.

Please for help!

#436899

1. It is not supported or inteded to create a JSON output with Views.

2. Views is a Data RENDER engine and not a Data PROVIDER engine

3. The best approach is to create a Custom ShortCode where you create the JSON output on your own.

4. Despite all this it is possible to create JSON with Views, but on your own responsibility and knowing the side-effects:

- Every Custom Filter, pagination, and whatsoever nice feature of Views is maiinly based on a HTML DIV that wraps the View Loop.

- This Wrapper is exactly what makes it impossible to create JSON with Views

- This Wrapper can be removed by this function:
https://toolset.com/forums/topic/views-not-working-after-installing-v1-11/#post-345294

- But then many things will NOT work in Views, and this we can not change.

- Another problem is, if the view where to reurn NO results (maybe nothing matches your query and "no results found" is triggered", your above Custom Function will BREAK because it applies only to the HTML COMMENTS "<!-- wpv-loop-start -->" and "<!-- wpv-loop-end -->"

- So that means, to apply the same filter to the "no results found" you need to add 2 more HTML comments to your View loop, wrapping the "no results found" section in the Loop.

- Then you would apply the same Custom Function as above, but changing the addressing HTML Comment to your Custom Comment.

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--noloop-start -->' );
        if ( 
            $start !== false
            && strrpos( $out, '<!-- wpv-noloop-end -->', $start ) !== false
        ) {
            $start = $start + strlen( '<!-- wpv-noloop-start -->' );
            $out = substr( $out , $start );
            $end = strrpos( $out, '<!-- wpv-noloop-end -->' );
            $out = substr( $out, 0, $end );
        }
    }
    return $out;
}

And then modify the LOOP in the View:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
<!-- wpv-noloop-start -->
		
	[/wpv-no-items-found]
<!-- wpv-noloop-end -->
[wpv-layout-end]

As you see now I male sure also the "no results" section would output a "clean" result.

As for Conditional inside the Loop in JSON case, this should not be a cause of issues, as long that Conditional also works on a native View.
Does it?
I see you use Custom ShortCodes in there.

So - even if I can not debug such a customized View output, I can suggest to frist try this in a "native" view - if it works, it should also work in a modifed View, but unfortunately as said, I can not go in depth debuggin here, as we basically do not support this modifcation of Views.

#436911

I think, I understand, what you are suggesting. And I now about the side-effects. But I didn't mean to have the "no-results" message on my page. (But it is also good to know, how I can change that *fg*).

I wanted to filter the view-results before cleaning up for the JSON-output. My views-list should render all elements, where the date-field matches a specific date (that works) OR where the date-field is empty (that doesn't work). In another view the [wpv-conditional] as I mentioned above did this trick for me. But this breaks the generic-field in CRED. And the integrated views-filter (as in my screenshot) doesn't let me filter for empty fields, does it?

But anyhow, I think, you brought me on the right path. I will try to remove the [wpv-conditional]-lines also with the "prefix_clean_view_output". I think, that could work … I will try and let you know.

#437118

Hello Beda!

I overlooked your last few sentences, where you said, that a conditional inside the loop shouldn't break my JSON-output. I have the same conditional in another view and it is working with my custom shortcode. But I tried the conditional without my custom shortcode - that doesn't work either.

I posted the output of my JSON-view as text in the CRED-form. Here it is:

{"value":"Launch-Party","label":"Launch-Party"} ,{"value":"Bergerler-Tage","label":"Bergerler-Tage"} [/wpv-conditional] ,{"value":"Bauernherbst","label":"Bauernherbst"} [/wpv-conditional]

You can see, the output of my first item is correct, but the output of all other items contains the "[/wpv-conditional]" in the end. And the items aren't filtered by my condition (all items are still displayed in my output). What do I miss here?

Here again my loop-code:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-conditional if="( $(wpcf-akt-bis) eq '' )"]
            [wpv-item index=1]{"value":"[wpv-post-title format='meta' meta='ID']","label":"[wpv-post-title format='meta' meta='nickname']"}
            [wpv-item index=other],{"value":"[wpv-post-title format='meta' meta='ID']","label":"[wpv-post-title format='meta' meta='nickname']"}
          [/wpv-conditional]
		</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
	<!-- wpv-noloop-start -->
		<div><h4 class="centering">[wpml-string context="wpv-views"]Derzeit bieten wir keine Aktionen an.[/wpml-string]</h4></div>
	<!-- wpv-noloop-end -->
	[/wpv-no-items-found]
[wpv-layout-end]
#437120

Another hint: I tried to post the output without "wpv-item index" - this way:

<!-- wpv-loop-start -->
  <wpv-loop>
          [wpv-conditional if="( $(wpcf-akt-bis) gte [my-today] ) OR ( $(wpcf-akt-bis) eq '' )"]
          	{"value":"[wpv-post-title format='meta' meta='ID']","label":"[wpv-post-title format='meta' meta='nickname']"},
          [/wpv-conditional]
  </wpv-loop>
<!-- wpv-loop-end -->

That is working! It's filtering correctly and the output is right:

"value":"Launch-Party","label":"Launch-Party"}, {"value":"Bergerler-Tage","label":"Bergerler-Tage"}, {"value":"Bauernherbst","label":"Bauernherbst"},

But now, I don't know how to get rid of the last comma in the line, so that it works in the generic field … Hmmm … Any ideas?

#437125

I did it! 🙂 I had to write the conditional in each wpv-item-line separately. Now it works fine!

Just for the record, here's the code in my loop:

<!-- wpv-loop-start -->
		<wpv-loop>
          [wpv-item index=1]
            [wpv-conditional if="( $(wpcf-akt-bis) gte [my-today] ) OR ( $(wpcf-akt-bis) eq '' )"]
              {"value":"[wpv-post-title format='meta' meta='ID']","label":"[wpv-post-title format='meta' meta='ID']"}
            [/wpv-conditional]
          [wpv-item index=other]
            [wpv-conditional if="( $(wpcf-akt-bis) gte [my-today] ) OR ( $(wpcf-akt-bis) eq '' )"]
              ,{"value":"[wpv-post-title format='meta' meta='ID']","label":"[wpv-post-title format='meta' meta='ID']"}
            [/wpv-conditional]        
		</wpv-loop>
	<!-- wpv-loop-end -->
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.