Skip Navigation

[Resolved] Conditional test – Grandparent post-id against a second post-id

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to create a conditional that tests the ID of a parent post against the ID of another post created by the current User. I can use Views with post relationship filters and I can use nested Content templates to display the correct IDs, but the conditionals always fail because there is extra space around the ID output by the View.

Solution: You can use the new View option that removes the wrapping div around the output, or you can use the older filter that strips those extra elements. If you use the older filter, you must manually delete all line breaks and spaces from the View's loop editor.

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 5 years, 3 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 8 replies, has 2 voices.

Last updated by tony 5 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1174577

I am trying to display a CRED form for a Craft Post only if the parent post-id of the Craft Post is the same as the grandparent post-id of the logged in user.

I have got as far as finding the two post IDs for the conditional test but there is something stopping an equal compare (which I think is to do with the need to use a template when finding the grandparent post ID). Do I need an additional filter to clean the template?

The grand parent of the logged in user is found via 3 Post Types:

Family-Group
Person
Message Control

Logged in Users are linked to Message Control (via Author) and Message Control has a 1 to 1 relationship with Person.

I have created a View that gets the Post ID of the Family Group applicable to the User. That view is cleaned via a filter.

[wpv-view name="get-post-id-of-family-group-belonging-to-logged-in-user"]

Select 1 Message Control post with the author the same as the current logged in user.

The loop content is:

[wpv-post-body view_template="Parent Family-Group Content Template get parent ID" item="@message-control-person.child"] 

The template content is:

[wpv-post-id item="@family-group-person.parent"]

The View is post 15459 and the filter code is at the end of this message.

The second post id is a simple types code:

[wpv-post-id item="@family-group-craft-registration.parent"]

So, I want any logged in user belonging to a Family-Group to be able to edit the content of any Craft post that is linked to the same Family-Group.

My conditional test is:

[wpv-conditional if="( '[wpv-view name='get-post-id-of-family-group-belonging-to-logged-in-user']' eq '[wpv-post-id item='@family-group-craft-registration.parent']' )"]equal[/wpv-conditional]

[wpv-conditional if="( '[wpv-view name='get-post-id-of-family-group-belonging-to-logged-in-user']' ne '[wpv-post-id item='@family-group-craft-registration.parent']' )"]not equal[/wpv-conditional]

The result is that all conditionals are returning "not equal".

/************************************************** 
* For View 15459 clean up output ready for a conditional test
**************************************************/

add_filter( 'wpv_filter_wpv_view_shortcode_output', 'tssupp_naked_view_output', 5, 2 );
 
function tssupp_naked_view_output( $out, $id ) {
    if ( $id == '15459' ) {
        $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;
}
#1174872

The View is post 15459 and the filter code is at the end of this message.
This filter is probably not necessary anymore. In the View editor screen there is now a checkbox to "Disable the wrapping DIV around the View". It'll strip out all the extra comments and spaces.

[wpv-conditional if="( '[wpv-view name='get-post-id-of-family-group-belonging-to-logged-in-user']' eq '[wpv-post-id item='@family-group-craft-registration.parent']' )"]equal[/wpv-conditional]

One thing I see here is there are single quotes nested inside other single quotes, which can cause parsing issues. As a test, please turn debug mode on in both conditionals. You can turn debug mode on by adding debug="true" to the end of the wpv-conditional shortcodes:

[wpv-conditional if="( '[wpv-view name='get-post-id-of-family-group-belonging-to-logged-in-user']' eq '[wpv-post-id item='@family-group-craft-registration.parent']' )" debug="true"]equal[/wpv-conditional]

[wpv-conditional if="( '[wpv-view name='get-post-id-of-family-group-belonging-to-logged-in-user']' ne '[wpv-post-id item='@family-group-craft-registration.parent']' )"]not equal[/wpv-conditional]

Then reload the page on the front-end of the site. You should see some extra information displayed on the front-end. Copy and paste that in your next reply. If no extra information is displayed, that indicates a parsing issue in your conditional clauses. We can discuss some alternatives to your current approach if necessary.

Do I need an additional filter to clean the template?
Probably not, but you can try turning on raw output mode by adding it to the post body shortcode like this:

[wpv-post-body view_template="Parent Family-Group Content Template get parent ID" item="@message-control-person.child" output="raw"]
#1174886

Hi Christian

Many thanks for your help with this - much appreciated.

I have done everything except remove the filter - the debug code is below.

============================
####################
wpv-conditional attributes
####################
Array
(
[if] => ( '

15066

' = '15066' )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( '

15066

' = '15066' )
--------------------
After replacing 1 general variables and comparing strings: ( '

15066

' = 15066 )
Comparing

15066

to 15066

not equal

####################
wpv-conditional attributes
####################
Array
(
[if] => ( '

15066

' ne '15066' )
[debug] => true
)

####################
Debug information
####################
--------------------
Original expression: ( '

15066

' ne '15066' )
--------------------
After replacing 1 general variables and comparing strings: ( '

15066

' ne 15066 )
Comparing

15066

to 15066
===============================================

This will mean more to you than me!

Kind regards
Tony

#1175154

It looks like there is still some empty space causing the comparison to fail. Try manually removing all the spaces and line breaks from the View's Loop editor. If a template is used in the Loop, remove all spaces and line breaks from that template as well. If this doesn't resolve the problem, I'll take a closer look.

#1175181

Thanks Christian

Yes - I think there is something probably in the View/Template but everthing has been removed in the code - no spaces or line feeds. My guess is it's something to do with using:

<wpv-loop>[wpv-post-body view_template="Parent Family-Group Content Template get parent ID" item="@message-control-person.child" output="raw"]</wpv-loop>

The template has the following content with no line feed:
[wpv-post-id item="@family-group-person.parent" output="raw"]

I have done these comparisons before but never used post-body with a template.

Your help much appreciated.

Kind regards
Tony

#1175198

Please provide login credentials in the private reply fields here, and I'll take a closer look. Let me know where I can see the conditional on the front-end of the site.

#1176095

Okay I think there was a miscommunication, because line breaks and spaces were still present in the View's Loop Editor. Check the View now hidden link

Here is the old Loop Editor code:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>[wpv-post-body view_template="Parent Family-Group Content Template get parent ID" item="@message-control-person.child" output="raw"]</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Here is the updated Loop Editor code with all the extra spaces and line breaks removed:

[wpv-layout-start][wpv-items-found]<!-- wpv-loop-start --><wpv-loop>[wpv-post-body view_template="Parent Family-Group Content Template get parent ID" item="@message-control-person.child" output="raw"]</wpv-loop><!-- wpv-loop-end -->[/wpv-items-found][wpv-no-items-found]<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>[/wpv-no-items-found][wpv-layout-end]

Now it looks like the comparison is working as expected. Can you confirm?

#1176126

Ah!

I did not go far enough removing lie feeds and spaces - sorry about that.

It's working fine now so many thanks for your help and hopefully others may benefit as well.

Kind regards
Tony

#1176145

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.