I have a view, and need a conditional output like this (in the loop item):
[wpv-conditional if="( '[wpv-post-id]' ne '145' )"][wpv-post-title][/wpv-conditional]
This works nicely, but rather than hardcoding the post id '145', I would like instead to use the id of a post output by another view used on the same page.
Essentially, the logic is this:
"in the loop, output the title of the post, unless the post has been already output by another view used on this page."
Does that make sense? And is it possible?
Hi Andrew,
Thank you for contacting us and I'd be happy to assist.
I'm afraid, there is no built-in method available to achieve this, so it will require some workaround/customization.
To suggest the best way forward, I'll need to see how this page and its views are set up in the admin area.
Can you please share temporary admin login details, along with the link to an example page?
Note: Your next reply will be private and please make a complete backup copy, before sharing the access details.
regards,
Waqar
Hi Andrew,
Thank you for waiting and I apologize for the delay in getting back on this.
I noticed that your view "Featured In focus widget" is set to show "In focus" posts, where the "Featured" field is checked (and thus has the value "1").
If you'd like to exclude all such posts from your other view "In focus landing", you can simply include a query filter for this "Featured" field, to bring in only those posts which don't have this field checked (and thus has the value "0").
( screenshot: hidden link )
Note: For unchecked state, "0" is automatically saved for this field, since in the field's settings "When unchecked, save 0 to the database" option is selected.
After adding this query filter for the "Featured" field, you'll no longer have to include a conditional check in the view's output.
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Many thanks for your reply, Waqar. It is much appreciated!
Your suggested solution is, unfortunately, not quite applicable, for the following reason:
I do want to include posts with the "featured" checkbox selected, in the view. The only post I wish to exclude is the most recent post with the "featured" checkbox selected. All others posts should be included in the view.
So the simplified logic is this: include all posts in the view, except the most recent "featured" post.
Does that make sense? Many thanks, Andrew
Hi Andrew,
Thank you for sharing these details and clearing the requirement.
I was able to make this work on your website, by following these steps:
( it can be seen on the page named "Test page from waqar" )
1. I've updated the view "Featured In focus widget" so that it only outputs the ID of the post that needs to be excluded, through the [wpv-post-id] shortcode.
2. In the view "In focus landing", I've removed the custom field query filter for the "Featured" field and then excluded the post using the "wpv_filter_query" filter:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )
add_filter( 'wpv_filter_query', 'filter_posts_order_fn', 1000 , 3 );
function filter_posts_order_fn( $query_args, $view_settings ) {
// if specific view
if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 39) ) {
// get the ID of the post to exclude
$post_to_exclude = trim(do_shortcode('[wpv-view name="features-in-focus-widget"]'));
// if the ID of post to exclude exists, exclude it from the query
if(!empty($post_to_exclude)) {
$query_args['post__not_in'][] = $post_to_exclude;
}
}
return $query_args;
}
Please note how this function gets the ID of the target post from the view "Featured In focus widget" and updates it in the query of the other view ("In focus landing") so that it can be excluded.
( you'll find this code added in the active child theme's "functions.php" file )
Note: I'm not recommending the approach of using conditional output comparison ( [wpv-conditional] ) in this case because it would show the incorrect number of posts on page 1, as the main view is using pagination.
Note: The custom code examples from our forum are shared to get you started in the right direction. You're welcome to adjust them as needed and for more personalized customization assistance, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar
Thank you Waqar! I greatly appreciate your patience and perseverance in solving this challenge.
Regards,
Andrew