Skip Navigation

[Resolved] Featured posts at the top of the list, part 2

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to be able to set certain posts as "featured posts", so they will appear at the top of sorted results lists.

Solution: Use a custom field to store a value of 1 for all posts, or 2 if the post is "featured". Use the cred_save_data hook to add this value automatically when posts are created by CRED. Filter based on this custom field.

Relevant Documentation: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 23 replies, has 2 voices.

Last updated by Bochnacki 7 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#559391

In reference to https://toolset.com/forums/topic/featured-posts-at-the-top-of-the-list/
My idea of using JS

$('li.in-top').prependTo("ul.posts-list");

unfortunately is bad. When the pagination appears, the "Featured posts" section is broken down into pages according to the date of posting.

Maybe someone has some other idea how to make a "Featured posts" section always at the top of the result list?
What is important: to be searched through search filters like the rest of posts.

#559533

Hi, can I see this on your site somewhere? I am not quite sure I understand how it works. I have read the other post, but I don't see an example anywhere.

#559558

The example is on page hidden link
Posts made up are 3 posts on a gray background.
Now they are still added at the top of the list with JS.

#559632

unfortunately is bad. When the pagination appears, the "Featured posts" section is broken down into pages according to the date of posting.
I'm not able to see any pagination, only 6 posts on 1 page, so I need your help to understand.

1. On the first page of results, what do you expect to see? Right now I see:
- featured post 1
- featured post 2
- featured post 3
- post 4
- post 5
- post 6

2. Assume there are 3 pages of results. On results page 2, what do you expect to see? The same featured posts at the top, or a different group of featured posts, or no featured posts?

3. Assume there are 3 pages of results. On results page 3, what do you expect to see?

#559677
featured-posts.jpg

Ok, I made 2 posts per page and I got the post "Audi Q3". As you can see it was not among the other honors, because the date of its addition is much earlier than the other ones.
I think I will best show on screen "featured-posts.jpg" what I mean.

#559762

I see now, thanks for the picture. I need to do a bit more testing, and then I will have a detailed answer for you tomorrow. Thanks for your patience.

#559986

As Luo was saying earlier, there is not a good way to sort by taxonomy term. The best way to sort these posts so that the featured posts appear at the top of the list is to use a custom field. It appears that, based on the other ticket recommendations, you are currently using a custom taxonomy term, "intop", to define featured posts.

I think you should consider using a custom field instead of a taxonomy term. If you need to keep the featured post taxonomy "intop" for other Views or other features, I can help you to set the value of this custom field whenever a post is saved using the save_post hook. We can test to see if the "intop" term is applied to this post type, and automatically update the custom field appropriately. Then you will be able to sort based on this custom field, and keep your taxonomy term in place.

This means that the "featured" status is really managed in two places, which isn't a good idea. If the taxonomy term "intop" is not used for any other functionality, I think you should stop using it and instead use a custom field radio button ("Featured" or "Not featured") to set the featured post status. Remove the "intop" term and replace it with the Featured status custom field. Then your "featured" status is only managed in one place.

Let me know how you would like to proceed.

#560006

Using the name "featured" in this thread, I wanted to simplify the explanation of the problem.
Now I see it was a mistake...

My taxonomy "featured" is used for different types of promotions posts, and the "intop" taxonomy is the one I want to add to the top of the list of posts.

I do not quite understand what you are proposing. I do not know how you want to connect a custom field with payment for this option.
Now the option (taxonomy) "intop" is connected to the payment through the CRED form:

add_action( 'woocommerce_order_status_completed', 'add_featured_option', 10, 1 );
function add_featured_option( $order_id ) {	
    $cred_form_id = get_post_meta($order_id, '_cred_form_id', true);
	$cred_form_ids = array(39, 47, 2072);// here you can add more CRED form IDs    
	if(	in_array($cred_form_id, $cred_form_ids) ){
        $order = wc_get_order( $order_id );	
        $post_id = get_post_meta($order_id, '_cred_post_id', true); 
		$terms = array();
        foreach ( $order->get_items() as $item ) {
            $product_id = $item['product_id'];
			if($product_id == 1174){ // this is product ID of "home"
				$terms[] = 795; // this is term ID of "inhome"
			}
			if($product_id == 981){ // this is product ID of "featured"
				$terms[] = 796; // this is term ID of "featured"
			}
			if($product_id == 1175){ // this is product ID of "intop"
				$terms[] = 797; // this is term ID of "intop"
			}
        } 
		if(!empty($terms)){
			$term_taxonomy_ids = wp_set_object_terms( $post_id, $terms, 'payment', true );
		}
	}
}

I suspect this information is changing things.

#560067

My taxonomy "featured" is used for different types of promotions posts, and the "intop" taxonomy is the one I want to add to the top of the list of posts.
I'm proposing you use a custom field instead of a taxonomy term for this. Is the "intop" term used for anything EXCEPT making these posts featured? If not, the solution is simpler. We will modify the code you already have in place. Instead of adding the "intop" term, we will set a custom field value on this post to "Featured". I can help you with this, it's option 1.

If the "intop" term is used in other places, for example if you have many Views with complex filtering in place that rely on the "intop" term, then the solution is a bit more complex. The code you have above will remain the same, and we will add some more code using the "save_post" hook:
https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

In the save_post hook, we will check to see if the "intop" term exists on the post. If it does, we will set our custom field value to "Featured". If it does not, we will set our custom field value to "Not featured". This way, the custom field is always in sync with the taxonomy term, even if you decide to remove the "intop" term later. I can help with this too, it's option 2.

So option 1 or option 2. It really depends on how you use the "intop" term in the rest of the site. I hope this helps clear things up.

#560109

All three post promotion methods ("featured", "inhome" and "intop") are used in several Views with parametric filtering. These are Views of different categories.

"Is the "intop" term used for anything EXCEPT making these posts featured?" - No. It is only intended to move/show posts at the top of the list of all posts.

I do not know how to do that, so if you can help, then the choice of method is up to you 🙂

#560129
order.png
in-top.png

Okay here are the steps you must take:
1. Add a new custom field to your post type. This should be a radio button. The options should be "Normal" = 1, and "In Top" = 2. You can change the names if you want, but the number values should remain 1 and 2. Set "Normal" to be the default. See the attached screenshot "in-top.png"

2. Change your woocommerce_order_status_completed hook. Delete this line:

$terms[] = 797; // this is term ID of "intop"

3. Replace it with this code:

update_post_meta( $post_id, 'wpcf-in-top', 2);

Replace 'in-top' with your custom field slug, using the 'wpcf-' prefix.

4. Test the CRED Commerce form, using product ID 1175. Complete the order, and you should find the custom field set to "In Top" in your post. The "intop" term should not be applied.

5. Edit your View to "Order by: Field - In Top, Descending, As a number". See order.png.

6. Test the View. The In Top items should appear at the beginning of the results set.

Let me know how it goes.

#560147

I did as you wrote. The post is empty.

#560373

Okay I would like to take a look in your wp-admin area to see why this is not working. If that's okay with you, please provide login credentials in the private fields here.

#560453

Thank you, I will wait until you tell me to continue before I make a change. Can you tell me which CRED form is used to create these Osobowe posts, and where I can see this form on your site?

#560489
how-add-osobowe.jpg

hidden link - CRED form.
On screen "how-ad-osobowe.jpg" I show how to open forms.