Skip Navigation

[Resolved] Conditional check for string in body of child post

This support ticket is created 2 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.

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by szymonF 2 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2265791

This is a continuation from this thread, where Waqar offered great help: https://toolset.com/forums/topic/get-the-conditional-to-check-if-the-post-body-contains-a-string/

The idea was to scan post text for words such as 'Abstract:', 'Publisher's note:', etc. - if they don't appear, the conditional display will add them, but if they do appear, the conditional display won't add them.

I want to use the same solution in different views. In one of them, the posts displayed are just posts, and it all works fine. Thank you Waqar 🙂 Here is an example: hidden link entry: Midgley, Mary. Individualism and the Concept of Gaia. This is the code I am using:

          [wpv-conditional if="( '[body_text_contains needle='Abstract:']' eq '0' ) AND ( '[body_text_contains needle='Note:']' eq '0' ) AND ( '[body_text_contains needle='note:']' eq '0' ) AND ( '[body_text_contains needle='Matter:']' eq '0' ) AND ( '[body_text_contains needle='matter:']' eq '0' ) AND ( '[body_text_contains needle='Syllabi']' eq '0' ) AND ( '[body_text_contains needle='Introduction:']' eq '0' ) AND ( '[body_text_contains needle='Content:']' eq '0' ) AND ( '[body_text_contains needle='Summary:']' eq '0' ) AND ( '[body_text_contains needle='Conclusion:']' eq '0' )"]
            [wpv-conditional if="( $(wpcf-medium) eq '1' )"]<strong>Publisher's Note: </strong>[/wpv-conditional]
            [wpv-conditional if="( $(wpcf-medium) ne '1' )"]<strong>Abstract: </strong>[/wpv-conditional]
          [/wpv-conditional]
          [wpv-post-body view_template="None" suppress_filters="true" output="raw"]

In the other view, the posts are part of a relationship, so what is displayed is an intermediary post that draws data from the child post. Here is an example: hidden link entry: Midgley, Mary. Individualism and the Concept of Gaia. Unfortunately, here the code is not working and I get the word 'Abstract:' twice - once because it's part of the post, the second time added by the content template. This is the code I am using:

          [wpv-conditional if="( '[body_text_contains item='@blueprint-text.child' needle='Abstract:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Note:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='note:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Matter:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='matter:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Syllabi']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Introduction:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Content:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Summary:']' eq '0' ) AND ( '[body_text_contains item='@blueprint-text.child' needle='Conclusion:']' eq '0' )"]
            [wpv-conditional if="( '[wpv-post-field name='wpcf-medium' item='@blueprint-text.child']' eq '1' )"]<strong>Publisher's Note: </strong>[/wpv-conditional]
            [wpv-conditional if="( '[wpv-post-field name='wpcf-medium' item='@blueprint-text.child']' ne '1' )"]<strong>Abstract: </strong>[/wpv-conditional]
          [/wpv-conditional]
          [wpv-post-body view_template="None" suppress_filters="true" output="raw" item="@blueprint-text.child"]

I worry it's some simple syntax error somewhere, but I just can't see it. Am I missing something?

#2266421

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

The custom code example shared in the previous ticket ( ref: https://toolset.com/forums/topic/get-the-conditional-to-check-if-the-post-body-contains-a-string/#post-2212611 ) only supports checking the post body/content from the current post. It doesn't support the use of the "item" attribute to check the posy body/content from some other post.

To make it support the 'item' attribute too, you'll need to update the custom shortcode slightly:


add_shortcode('body_text_contains', 'body_text_contains_func');
function body_text_contains_func($atts) {

	$a = shortcode_atts( array(
		'needle' => '',
		'item' => '',
	), $atts );

	if (!empty($a['item'])) {
		$content = do_shortcode("[wpv-post-body view_template='None' suppress_filters='true' item='".$a['item']."']");
	} else {
		$content = do_shortcode("[wpv-post-body view_template='None' suppress_filters='true']");
	}

	$pos = strpos($content, $a['needle']);

	if($pos >= 1){
		return '1';
	} else {
		return '0';
	}
	
}

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2266797

Fantastic, this works a dream. Thank you!!!

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