Skip Navigation

[Résolu] Ordering a view by values in a shortcode

This support ticket is created Il y a 7 années et 6 mois. 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.

This topic contains 3 réponses, has 2 voix.

Last updated by Konstantinos Galanakis Il y a 7 années et 6 mois.

Assisted by: Konstantinos Galanakis.

Auteur
Publications
#446181

Hi,

I have setup a parent and child post relationship, and also a custom shortcode / sql query which returns a number based on the number of associated child posts of a certain criteria.

I then have a WP-Types view loop that lists each parent post, and the associated numerical value of the shortcode. i.e.

<!-- wpv-loop-start -->
<wpv-loop>
[wpv-post-link] ([my_shortcode])
</wpv-loop>
<!-- wpv-loop-end -->

The view works well and looks like this

Parent A (14)
Parent B (5)
Parent C (87)
Parent D (32)

However, I would like to order the view by the numbers returned in the shortcode, in descending order so that it looks like this:

Parent C (87)
Parent D (32)
Parent A (14)
Parent B (5)

I understand there is a way to setup a custom sort order based on my shortcode using PHP and the "posts_orderby" filter, but would appreciate some help in writing the PHP code, and i assume i just add the custom code to my "functions.php" file?

Thanks for your help.

#446508

Hello Anthony.

Thank you for contacting the Toolset Support.

In order to achieve this, you need to follow the steps below

1) Add a number custom post field to your parent custom post type, for example, with slug "count".
2) Now in your functions.php file add the code presented below, that updates that parent custom post field whenever a child post type is saved. All it does is to evaluate the available children posts for the parent selected in the current child post. Also, updates that count when a post is deleted. Remember to replace the custom post type slugs. Here parent is slug 'clinic' and child slug is 'department'.

add_action( 'before_delete_post', 'count_total_children_delete' );
function count_total_children_delete($post_id)
{
    if ( get_post_type( $post_id ) == 'department' ) {
        $parent_id = get_post_meta( $post_id, '_wpcf_belongs_clinic_id', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'department',
                'numberposts' => -1,
                'meta_key' => '_wpcf_belongs_clinic_id',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_belongs_clinic_id', 'value' => $parent_id))
            );
            $totalChildCount = count(get_posts($childargs));

            update_post_meta( $parent_id, 'wpcf-count', $totalChildCount);
        }
    }
}

add_action( 'save_post', 'count_total_children' );
function count_total_children($post_id){
    if ( get_post_type( $post_id ) == 'department' ) {
        $parent_id = get_post_meta( $post_id, '_wpcf_belongs_clinic_id', true );
        if($parent_id != '')
        {
            $childargs = array(
                'post_type' => 'department',
                'numberposts' => -1,
                'meta_key' => '_wpcf_belongs_clinic_id',
                'orderby' => 'meta_value',
                'order' => 'ASC',
                'meta_query' => array(array('key' => '_wpcf_belongs_clinic_id', 'value' => $parent_id))
            );
            $totalChildCount = count(get_posts($childargs));

            $oldChildCount=get_post_meta( $parent_id, 'wpcf-count', true );
            if( $totalChildCount != $oldChildCount ){
                update_post_meta( $parent_id, 'wpcf-count', $totalChildCount );
            }
        }
    }
}

3) Now whenever you create a new parent post, just keep in mind to add a 0 to that custom post field in order to reset the counter.
4) Now create a view listing your parent custom post type posts, order it by the "count" custom post field (descending) and use the simple loop below

<wpv-loop>
	<li>
		[wpv-post-title] ([wpv-conditional if="( $(wpcf-number) ne '' ) OR ( $(wpcf-number) ne '0' )"][types field='number' format='FIELD_VALUE'][/types][/wpv-conditional][wpv-conditional if="( $(wpcf-number) eq '' ) OR ( $(wpcf-number) eq '0' )"]0[/wpv-conditional])
	</li>
</wpv-loop>

5) Go and create/update your children posts in order for the counters to be updated in the parent posts.

Also, keep in mind that you also have to deal with the case when a user changes parent for one of the child posts. You have to decrease the count for one parent and increase the count for the new one. It won't be that hard to do it yourself.

If you have any further issues, please let me know.

Regards

#446754

Thank you for the response Konstantinos, but the above solution doesn't work i am afraid. The shortcode i am working with is the result of a custom SQL query, so just counting the total number of child posts added / deleted will not provide the correct result.

Also, you mention having to:

"keep in mind that you also have to deal with the case when a user changes parent for one of the child posts. You have to decrease the count for one parent and increase the count for the new one. It won't be that hard to do it yourself."

This is also not feasible, as i am expecting a large number of posts to be added / amended on the site, so having to manually update parent posts each time is not going to work.

Please provide an automated solution that will work with a shortcode that contains a numerical value. I would hope this is possible from the look of a few other similar forum posts such as below.

https://toolset.com/forums/topic/taxonomy_orderby-order-taxonomies-terms-by-shortcode-attribute-in-view/

Thanks again

#447356

Hello.

It seems, unfortunately, that for now, you need custom programming work which is beyond the scope of our support.

At this point I would suggest you consider contacting one of our certified partners from this link:
https://toolset.com/consultant/

You will get the custom assistance you need to get on with your project.

If you have any further issues, please let me know.

Regards

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