Skip Navigation

[Resolved] Do not display latest post with custom field value

This support ticket is created 7 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 8 replies, has 2 voices.

Last updated by johnU 7 years, 3 months ago.

Assisted by: Minesh.

Author
Posts
#483424

I have created a CPT. In the post fields is a radio selection with two options; "yes" and "no".

In the view created for the CPT, i want to show all posts except the latest post with "yes" selected in that field (I am showing that elsewhere on the page).

How can I do this? Is there a conditional statement or filter that I can use?

#483518

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

As I understand you want to filter your CPT entries with custom field value set to "YES" - correct?

Please look at the following Doc which illustrates how you can filter your view by custom field.
More info:
=> https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

i want to show all posts except the latest post with "yes" selected in that field (I am showing that elsewhere on the page).
==> I need more explanation with the above line, may be a test case example will be enough to understand your exact requirement.

#483609

My CPT has a field where there are two options to the field; "yes" or "no".
Let's say I create 10 posts with the following options chosen...

Post 1 "Yes"
Post 2 "No"
Post 3 "No"
Post 4 "Yes"
Post 5 "No"
Post 6 "No"
Post 7 "No"
Post 8 "Yes"
Post 9 "Yes"
Post 10 "No"

I would like to create a view that does not display the latest post that has chosen "yes" (post 9). So this view will show all entries except Post 9.

#483610

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

So everytime it will check for only 1 latest post which have option set as "Yes" and this latest entry should not be display by view?

#483611

Yes exactly. All other posts will be displayed except for that one post.

#483618

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should use view's filter hook: wpv_filter_query

Using this hook you can fetch the specific post id which is latest and custom field value set 'YES' and then use 'post__not_in' argument with your query.

More info:
https://toolset.com/documentation/user-guides/views-filters/wpv_filter_query/

For example - something like as follows:

Add following code to your current theme's functions.php file and change following code as per guidelines:

add_filter('wpv_filter_query', 'featured_products_random_order', 10, 2);
 
function featured_products_random_order($query, $settings) {
    if ($settings['view_id'] == 9999) {
        global $wpdb;
        $id = WRITE QUERY TO GET POST ID of latest post with custom field set with value = yes
        $query['post__not_in'] = $id;
    }
    return $query;
}

Where:
- Replace 9999 with your original view ID.

#483657

Thanks Minesh, so this can only be done through custom code?

I was hoping there would be an option in views, since I would not know the first thing in writing the query string.

#483663

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - this is the best workaround which I offer you 🙂

#485297

Thanks Minesh.

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