Skip Navigation

[Resolved] How to create a list to display the last 5 CPT posts created

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.

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+01:00)

This topic contains 45 replies, has 2 voices.

Last updated by Nigel 5 years ago.

Assisted by: Nigel.

Author
Posts
#1189283

I am trying to: Display a list with the last 5 Profile CPT posts created by all users, unless the post by the current logged in user.

Link to a page where the issue can be seen: hidden link

I expected to see: the list with all posts created unless the post ceated by the current logged in user. With featured image and other meta.

Instead, I got: a list with 5 items, but only the current logged in user post... and the featured image is not displayed and all other meta are displayed with issue...

#1189646

Nigel
Supporter

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

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

You can add a Query Filter by author where you specify that the author is the currently logged-in user, but you cannot specify that the author is not the currently logged-in user, which is what you need.

But a Query Filter for post id does let you specify whether the posts returned should be those with the indicated ids, or whether the indicated ids should be excluded from the results.

So, one alternative is to create a View that returns the posts of the currently logged-in user, and then exclude that list of posts from your main View. However, while testing that solution I ran into some problems—I think there may be a bug, and I've raised it with my colleagues.

In the meantime, you can easily enough exclude the posts where the current user is the author by adding a code snippet (at Toolset > Settings > Custom Code), like so:

function tssupp_filter_query($view_args, $view_settings, $view_id) {

	if (in_array($view_id, array(182))) { // Edit View ID

		$view_args['author'] = null;
		$view_args['author__in'] = null;
		$view_args['author__not_in'] = get_current_user_id();

	}

	return $view_args;
}
add_filter('wpv_filter_query', 'tssupp_filter_query', 101, 3);

You'll need to edit the ID of the View in question.

First check that you see the expected posts, and if you still see problems with the content of the posts, let me know.

#1189655

Thanks, and what you mean with "Edit View ID"?
It need to change the "$view_id" with the number of the view or it need to change the value in the "array(182)" where 182 should be the View number?

#1189657

Nigel
Supporter

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

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

If you go to Toolset > Views you will see that your View has an ID.

On my test site the View ID was 182, which is what you need to change in the example code and replace it with the ID of your View.

#1189701

Thanks.
I have add the code and run it, but in this page hidden link where the View is applied I see always the same issue.
I cannot display the Featured Images, and it is displayed the same post for 5 times, when in the Profile CPT there are 5 posts...
The age shortcode don't display the correct age...

I don't know what is wrong. In other sites where I have used Tooset and View, these settings works fine...

#1189711

Nigel
Supporter

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

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

Maybe I could look at your site because from the front end I cannot see what the problem might be.

I will mark your next reply as private so that I can get log-in credentials from you—you may want to create a temporary admin user for me to use that you can later delete. And be sure to have a current backup of your site, even though I don't intend to make any changes.

#1189715
#1189885

Nigel
Supporter

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

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

I turned on debug mode for Views (in Toolset > Settings > Front-end Content) and then re-visited the test page.

Here is the relevant output, which I've edited to remove unnecessary information and highlighted the key information:

wpv_filter_query_post_process
WP_Query Object
(
    [query] => Array
        (
            [post_type] => Array
                (
                    [0] => profilo
                )

            [paged] => 1
            [suppress_filters] => 
            [ignore_sticky_posts] => 1
            [posts_per_page] => 5
            [post__not_in] => Array
                (
                    [0] => 10213
                )

            [wpv_original_limit] => 5
            [wpv_original_offset] => 0
            [wpv_original_posts_per_page] => -1
            [post_status] => Array
                (
                    [0] => publish
                )

            [orderby] => date
            [order] => DESC
            [author] => 
            [author__in] => 
****        [author__not_in] => 229 ****
        )

    [query_vars] => Array
        (
...
        )

    [tax_query] => WP_Tax_Query Object
        (
...
        )

    [meta_query] => WP_Meta_Query Object
        (
...
        )

    [date_query] => 

    [request] => SELECT SQL_CALC_FOUND_ROWS  wp_20_posts.ID FROM wp_20_posts  WHERE 1=1  AND wp_20_posts.ID NOT IN (10213) AND wp_20_posts.post_author NOT IN (229)  AND wp_20_posts.post_type = 'profilo' AND ((wp_20_posts.post_status = 'publish'))  ORDER BY wp_20_posts.post_date DESC LIMIT 0, 5

    [posts] => Array
        (
            [0] => WP_Post Object
                (
****                [ID] => 9729 ****
                    [post_author] => 235
...
                )

            [1] => WP_Post Object
                (
****                [ID] => 9693 ****
                    [post_author] => 233

                )

            [2] => WP_Post Object
                (
****                [ID] => 9003 ****
                    [post_author] => 116

                )

            [3] => WP_Post Object
                (
****                [ID] => 60 ****
                    [post_author] => 1

                )

        )

You can see that the query vars were successfully modified by the custom code you added (author__not_in 229, which is my ID), and you can see the list of posts that were returned by the query, which are not the ones being displayed on the page.

I added the post title directly in the loop output section of the View (not in the linked template which is designed with Divi) and these are being displayed correctly.

I don't want to interfere in your site too much, but I would delete what you have in the template designed with Divi and add elements back to it one at a time, starting with just the title, to see if you identify something which breaks this and stops the View from outputting the intended posts.

#1189891

Thanks Nigel,
Your snipped code work fine. So I have deleted the Content Template shortcode from the loop, and I have tryed to insert the image code, and it work fine.
So, I think there is a compatibility issue with Divi, but I don't know, because in other site built with Divi and Toolset, all work fine...

#1190020

I think I know what is the issue with Divi Builder, I remeber that I have the same issue in the other site when the Section is setted as global...
Now I'm redesigning the single user item to insert in the loop. After I'm trying to insert it without using the global settings, else, if it don't work fine, I clone the html structure and the css.

#1190352

Nigel
Supporter

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

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

I know that we have had issues raised when using Divi global modules, but I'm not sure exactly how you are using them to know what the specific problem is in this case.

I expect that if you go ahead and design the View template with Divi using standard modules you shouldn't experience any problems, but if you find some specific details you can share, let me know.

#1190464

Hi Nigel,
I have used the Divi Builder without the global settings, and it work fine, you can see.
But I have other two questions:
1- The simple question. why the url became hidden link, what is "?_tt=1548337426&_success=8383_1&_target=10319#" and what should I do to not display it?

2- The harder question. So if you see, I have added the heart icon, that it work to save the Profile CPT post ID and user id into the Favoriti CPT post. In the Profile CPT posts it work fine. On click on the heart the IDs are saved into the Favoriti CPT posts, and after the heart change its internal color to indicate that the profile post is added into favoriti. If I click another time to the heart icon, the Favoriti post with the profile IDs is deleted, and the heart icon became the initial icon without the internal color.
Now, in test page, when I click on the heart icon, yes the profile IDs are saved into a Favoriti post, but the heart icon don't change with the internal color heart icon, and if I click another time on the heart icon, the profile IDs are saved in a new Favoriti post... What should I do to make it work fine in this view loop also?

#1190781
per slide  incontrAmici.png

Hi Nigel,
in the attached image you can see the heart icon about the before issue question.

And, about the last row, I don't know why the loop generate the same value for all items... where every items have a different value in the field!
What is the problem and what should I do to solve it?

#1190896

Nigel
Supporter

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

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

I see the issues on your test page, I'm just starting and will need to look at how this is set up in the backend and will report back to you.

#1190952

Nigel
Supporter

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

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

Hi Francesco

The question of the URL parameters added when you use the heart icon to submit the favourite form, I don't have a solution for you. They are added in the circumstances how you use the form, i.e. with settings to keep displaying the form and a manual page reload. There is no way to prevent those from being added (I checked with the devs).

The problem with the favouriting form not working correctly when you click the heart icon on the profiles that are shown in the View of the latest 5 profiles is that your cred_delete_post_link and your favorito cred form use id="$current-page" when setting the profile id, meaning that it gets the id of the page where the View is inserted, not the ID from the current item in the View loop (which is the correct ID you require, if I have understood correctly).

I don't think that attribute is needed for this to work on the profile page either, can you not simply delete them?

The problem with the same location being displayed for each of the 5 posts is that you add that location info using a View ("localita-scheda-loop-singolo-utente"), and that View has a query filter to set the profile the citta is related to via a shortcode argument, but where you insert the View you don't supply the shortcode argument.

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