Skip Navigation

[Resolved] Order CPT with a new field

This support ticket is created 4 years, 2 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.

Our next available supporter will start replying to tickets in about 0.71 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by Shane 4 years, 2 months ago.

Assisted by: Shane.

Author
Posts
#1772345

Hello,

we have created a numeric field and added it to our CPT . In the WordPress Archive, we want to order the CPT with this new field. Unfortunately, nothing appeared to list from the existing CPT but if we create a new it appeared correctly.

How we can order our CPT with a new field?

Thank you.

#1772479

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aris,

Thank you for getting in touch.

Are you saying that this new custom field isn't appearing to be sorted by? however a newer custom field is appearing ?

Secondly have you created at least 1 post with that custom field value populated?

Please let me know.

Thanks,
Shane

#1772607

Hello,

Let me give you an example. I have 10 CPTs on a list without any order. I create a new checkbox and I added to the sort by option in the WordPress Archive. Then all CPT disappeared. I assume the value is null from them and this is the reason. If I create a new CPT with value in this new field its appeared correctly

We want to have a checkbox in the CPTs and when this checkbox is selected then appears at the top of the WordPress Archive list.

Thank you.

#1772673

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aris,

I understand what you mean now, in this case you will need to select another option for the checkbox for the posts that you don't want to appear on top.

This is because you can't sort posts by a null value, so all the other posts that don't have this checkbox checked won't appear.

I hope I was able to clarify this for you. So you can have 2 options Featured that stores a value of 1 and not featured that stores a value of 2.

Thanks,
Shane

#1773293

Hello Shane,

the problem is that we have 20k CPTs and we cant update them with value. Shouldn't toolset treat null as zero or something like this?

With this approach, we cant use any field to sort the Views

Thank you.

#1773341

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aris,

Unless the post itself has been saved with that field a database entry is not made for that post with that custom field. Its not that null isn't sortable its that the database entry doesn't exist unless the post has been saved at least once with that field.

What you can do as a workaround is create a second view that will only display those featured items and add it to the archive itself.

This way you can still have the featured items as well as the non-featured ones.

Thanks,
Shane

#1773441

Hello,

Ok, we have created the new view with only the needed values, but how we will exclude these from the other view? Because now are displayed twice? If we add the new field as a filter to our main View, again nothing appears.

Thanks.

#1773619

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Aris,

You can essentially use this filter in the link below.
https://toolset.com/forums/topic/how-to-list-post-based-on-non-custom-field/

In this case the customer is using a view. In your case you are using a custom archive. Essentially it will be like this.


function my_function( $query ){
    if ( is_post_type_archive( 'my_custom_post_type' ) ) {
       $meta_query = array(
                'key'     => 'wpcf-customfieldslug', 
                   'compare' => 'NOT EXISTS',
        );
 $query->set( 'meta_query', $meta_query );
    }
return $query;
}
add_action( 'pre_get_posts', 'my_function' );

What you need to do is to replace "wpcf-customfieldslug" with the slug of the custom field keeping the wpcf- prefix and 'my_custom_post_type' with the slug of your custom post type.

Please let me know if this custom hook helps.
Thanks,
Shane