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
#560528
which-product.png
product-id-45.png
wrong-product.png

Your CRED form is designed to add Product "Ogloszenie - standard" to the order. This Product has an ID of 45. Your code is written to only add the custom field value to Product ID 1175. Can you help me understand why these IDs are different?

See the attached screenshots.

#560547
intop.jpg

Term "intop" is a paid option associated with product ID 1175.
This option is activated with another CRED form - ID 3845 (screen "intop.jpg").

#560571

Which order was placed when you edited the "top top" post with CRED Commerce form 3845? These are the only orders I see that include product 1175, and the date when the order status changed to "Complete":
#4512 - 12 August
#4504 - 11 August
#4503 - 11 August
#4501 - 11 August
#4599 - 14 August
#4597 - 14 August
#4596 - 14 August

These were all "Complete" before I suggested the changes to your code. Please start from the beginning with new posts.

1. Submit CRED Commerce form 39 to create a new post. This adds product 45 into your cart.
2. Checkout, then mark the order complete.
3. What is the post ID created by CRED?
4. What is the order ID for your 1st WC order?
5. Now edit the post from #3 using CRED Commerce form 3845. This adds product 1175 to your cart.
6. Check out, then mark the order complete.
7. What is the order ID for your 2nd WC order?

Thank you.

#560587

Ok, I added new posts.

"3. What is the post ID created by CRED?
4. What is the order ID for your 1st WC order?
7. What is the order ID for your 2nd WC order?" - I do not understand

Now as the "Osobowe" view is set to "Order" on [Field - In Topp][Descending][As a number], only those posts that I activated as "In Top" with CERD from ID-3845 are visible.
All others are invisible.

Also the css class (the background of the post that has been "In Top") and the shortcode term "intop" (the "PODBIJ" button does not disappear) does not work, but that's understandable after the change to functions.php. I hope I can improve it later 🙂

("Order": [Field - In Topp][Descending][As a number] - I have to undo this setting for now, but you have access so if you need it you can change it)

#560841

7. What is the order ID for your 2nd WC order?" - I do not understand
When you click "PODBIJ" to submit CRED Commerce form 3845, product "Ogloszenie - Top" is added to your cart. Then you checkout, and a new order is generated. I was asking for the new order ID, but I don't think it's important now. You said the new Osobowe posts are appearing at the top of the list, so I think this is working correctly. By default, the posts should have the "Normal" value for this custom field. If CRED Commerce form 3845 is submitted, the post should have the "In Top" value for custom field. Please confirm this is working as expected.

Now as the "Osobowe" view is set to "Order" on [Field - In Topp][Descending][As a number], only those posts that I activated as "In Top" with CERD from ID-3845 are visible.
All others are invisible.

Old posts, created before the "In Top" field was added, will not have a saved value for this field, so they will not appear in the list. This can be resolved by resaving the old posts with the default "Normal" value for the custom field. All new posts will have the default value applied unless they are set to "In Top" with CRED Commerce form 3845, so they will all appear in the list. I hope this helps clear things up.

#560853

Now as the "Osobowe" view is set to "Order" on [Field - In Topp][Descending][As a number], only those posts that I activated as "In Top" with CERD from ID-3845 are visible.
All others are invisible.

I'm talking about new posts that have been added after the changes.
If they are not selected / marked as "in top" they will not appear on the list of posts.

#560900

I understand, if Osobowe posts are created in wp-admin, the default value will be set automatically. However I forgot that we need to set the default value when Osobowe posts are created by CRED.

There are two ways to set the default value:
1. Use cred_save_data hook to add the default value to any Osobowe post created by CRED:

add_action('cred_save_data', 'cred_add_default_in_topp',10,2);
function cred_add_default_in_topp ($post_id, $form_data)
{
    // IDs of all forms that create Osobowe posts
    $forms = array( 1, 2, 3 );
    if (in_array($form_data['id'], $forms))
    {
        update_post_meta($post_id, 'wpcf-in-topp', 1 );
    }
};

Modify the $forms array to include all the IDs of forms that create Osobowe posts.

2. Add the "in-topp" input field to your CRED forms and set its value to "1", then hide the input field using CSS.

#1 is probably the best option, but as you add and remove CRED forms you will probably need to update your hook to work with other form IDs.

#561142
in-top.jpg

It works, but the order is gone. Please, see screen "in-top.jpg".

#561629
setup-intop.jpg

My fault. I forgot to remove the code:

	$.each($('.in-top'), function() { 
		$(this).prependTo(".offers");
	});

How did I remove the order is correct.

The setting should be like on the screen "setup-intop.jpg".

Thank you very much for help.