Skip Navigation

[Resolved] view output in conditional output

This support ticket is created 6 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 11 replies, has 2 voices.

Last updated by udais 6 years, 3 months ago.

Assisted by: Nigel.

Author
Posts
#1076062

Tell us what you are trying to do?display post conditionally by querying other custom post

Is there any documentation that you are following?

Is there a similar example that we can see?

What is the link to your site?hidden link

#1076072

Hello

i have custom post : allowed users

Author of all posts (post type post) is admin. admin want access to view post only to those users who are allowed and stored in custom post allowed users.

in allowed users custom post there are two fields user id and post id

in user id field there and user id of users and post id field there are post id

ex: user id: 3 post id :160

if user 3(user id) is logged he wand to try to view post id 160 (posted by admin) then i want to query if user is allowed to view post which id is 160 by querying user id of current logged in user and current post user is trying to view by querying custom post allowed users.

I tried to achieve this by passing parameter in view "Allowed Posts-Check if user subscribed to post"

Select items with field:
user id is a string equal to VIEW_PARAM(userid)
AND
post id is a string equal to VIEW_PARAM(postid)

then i put following in post layout :
[wpv-conditional if="( '[wpv-view name="allowed-posts-check-if-user-subscribed-to-post" userid="[wpv-user field="ID"]" postid="[wpv-post-id]"]' eq '0' )"]user not subscribed to this post[/wpv-conditional]

but it does not work.

how to achieve this

#1076140

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screen Shot 2018-08-07 at 08.57.10.png

Hi there

Your conditional shortcode isn't working because the output of a View (from the wpv-view shortcode) contains a wrapper div and not just the View results (see screenshot), so testing for eq '0' is meaningless.

A Views update later this month will add the possibility to output a "raw" View without the wrapper, making such tests more straightforward.

If you need something like that now, you can add the following code to your theme's functions.php file to strip away the wrapper, and then your conditional shortcode can test for whatever content you include between the wpv-no-items-found shortcodes of your View output.

/**
 * Naked Views output
 */
add_filter( 'wpv_filter_wpv_view_shortcode_output', 'tssupp_naked_view_output', 5, 2 );
function tssupp_naked_view_output( $out, $view_id ) {
    $views_to_target = array( 123 ); // replace with comma separated list of View IDs to 'clean'
    if ( in_array($view_id, $views_to_target)) { // add test for View ID
        $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;
}
#1076196

Hello
Thanks for reply.

I added code in function.php but still not working.

Thanks

#1076204

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

But what did you update the wpv-conditional shortcode to?

#1076213

Hello

Sorry but I could not understand what you are asking.

I inserted code in functions.php but it give no output.

I removed all extra code.

[wpv-conditional if="( '[wpv-view name="allowed-posts-check-if-user-subscribed-to-post" userid="[wpv-user field="ID"]"
postid="[wpv-post-id]"]' eq '0' )"]user not subscribed to this post[/wpv-conditional]

link of post user not subscribed is : hidden link

There should be a message : user not subscribed to this post

but not working

Thanks

#1076232

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry if it wasn't clear, but the code I supplied, you need to edit the ID of the View that you need to "clean" the output of.

If the View has some posts it will output what is between the wpv-items-found shortcodes (in the Loop Output section of the View) and if the View contains no posts it will output what is between the wpv-no-items-found shortcodes.

Your wpv-conditional shortcode is testing if the View output eq '0', which it won't.

You can add the attribute debug="true" to your wpv-conditional shortcode and it will show what is being evaluated and compared (see https://toolset.com/documentation/user-guides/conditional-html-output-in-views/debugging/) which should help you set the right value in your test.

Or try outputting the View without the wpv-conditional shortcode so that you can see in the page source exactly what is output for it when a user doesn't have permission to see the post.

It might be easier to edit your View output to simplify what is included in the wpv-no-items-found section, which by default looks like this

	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]

but it might be easier to test for this

	[wpv-no-items-found]no[/wpv-no-items-found]
#1076254

I already edited the id of the view.

and added one more line and add view without condition and also added debug="true" but still it does not work.

view added without condition with debug=true displays : postid="167" debug="true"];

i can provide login details if you want to check

thanks

#1076259

I want to clear again basically i am querying custom post "allowed users"

which have two fields(meta keys) user id and post id.

what i am trying to do:

What i am trying to do:

if current logged in user visit post url to view post i want to check current logged in user and current post in allowed users custom post something like this
if logged-in-user and current post found in custom post "allowed user" . if record found then allow otherwise restrict some content of post. may be this can be achieved with some custom code counting custom post with current-logged-in-user and current-post>0 ithen allow else restrict some content

#1076341

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Let me set a private reply so I can take a look, if you like, as it doesn't sound like I've made myself understood.

#1077346

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I took a closer look at your condition and see that you were mixing up the double and single quotes, which I've tried to fix, but it's still not working, so much so that the debug="true" attribute doesn't even work.

It seems like the expression is too complex.

I would instead try writing a custom function to do the same thing instead of the View that can be used in the wpv-conditional shortcode.

I added the following function to your Code Snippets:

function post_is_allowed(){

	global $post;
	$user_id = get_current_user_id();

	$args = array(
		'post_type'		=>	'allowed-post',
		'post_status'		=>	'publish',
		'numberposts'	=>	1,
		'meta_query'	=> array(
			'relation'		=> 'AND',
			array(
				'key'		=>	'wpcf-user-id',
				'value'		=>	$user_id,
				'compare'	=>	'='
			),
			array(
				'key'		=>	'wpcf-post-id',
				'value'		=>	$post->ID,
				'compare'	=>	'='
			)
		)
	);
	$allowed = get_posts( $args );

	return count( $allowed );
}

That will return either 0 or 1.

I then registered that function at Toolset > Settings > Front-end Content to be able to use it in wpv-conditional shortcodes.

I then edited your Layout for posts and added a test row at the top which shows the use of this function inside conditions.

Visiting some of the posts on the front end shows it working correctly, if you could please verify.

So you should be able to delete the View you created and the custom code you added to your theme to "clean" that View.

#1078423

Thanks Nigel

It's working.