This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 8 replies, has 2 voices.

Last updated by Shekhar Bhandari 1 year, 4 months ago.

Assigned support staff: Shekhar Bhandari.

Author Posts
May 22, 2018 at 7:01 am #2205214

Martynas.V

I am trying to: apply WPML on workscout theme. One thing is not quite working - job count on home page header. For now there are 3 in English and some more in Lithuanian. But it both show that it have only 3. It says: "We have 3 job offers for you!"

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

I reached to the developer and he tried several things from the forum. Here is his response:
"I'm sorry but none of the functions I found on the - https://wpml.org/forums/topic/how-to-count-the-number-of-custom-post-types-in-a-specific-language/
- https://wpml.org/forums/topic/get-wp_count_posts-of-custom-post-type-per-language/
- https://wpml.org/forums/topic/count-number-of-translated-posts/
- https://wpml.org/forums/topic/wp_count_posts/

works, all return NULL, and there's no official help from WPML for this."

Could you advise if there is any solution to this?

May 23, 2018 at 4:56 am #2212674

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Thank you for contacting WPML support. I'd be happy to assist you further on this issue.

Regarding the posts count. That's something that WPML doesn't filter, and we have a feature request in our internal documentation to make it work. However, our second tier supporter has found a fix for that.

Could you please use the following function in your theme functions.php

function get_count_posts($post_type  ) {
     global $wpdb;
     $src_lang = isset( $_REQUEST['source_lang'] ) ? sanitize_text_field( $_REQUEST['source_lang'] ) : apply_filters( 'wpml_current_language', null );
     $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( $_REQUEST['lang'] ) : $src_lang;
     $wpml_type = "post_".$post_type;
     $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} as p join wp_icl_translations as ict on p.id = ict.element_id  WHERE post_type = %s and ict.language_code = %s and ict.element_type = %s GROUP BY post_status";

     $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $post_type, $lang, $wpml_type ), ARRAY_A );
     foreach ( $results as $row ) {
         $counts[ $row['post_status'] ] = $row['num_posts'];
     }
     $result = (object) $counts;

     return $result->publish;
}

and make sure to call the function using

get_count_posts('posttype');

Let me know if this helps.

Kind Regards

May 23, 2018 at 6:30 am #2212808

Martynas.V

Hello,

thank you for the snippet. Although it still not working. Maybe something should be change in template file?

<?php $count_jobs = get_count_posts('job_listing');

printf( _n( 'We have %s job offer for you!', 'We have %s job offers for you!', $count_jobs->publish, 'workscout' ), '' . $count_jobs->publish . '' ); ?>

May 23, 2018 at 6:32 am #2212810

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Could you just use $count_jobs and let me know if that helps?

Thanks

May 23, 2018 at 10:12 am #2214111

Martynas.V

I got it working but it shows incorrect number of jobs. Take a look: hidden link

May 23, 2018 at 11:10 am #2214676

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

To further debug this issue, I need to request temporary access (wp-admin and FTP) to your site in order to be of better help. You will find the needed fields for this below the comment area when you log in to leave your next reply. hidden link

These info is private, and available to you and supporters only. Read more about this: https://wpml.org/purchase/support-policy/privacy-and-security-when-providing-debug-information-for-support/

Note that:

#1
Backup (both files and database) your site before giving us your credentials. You can use a plugin for this if you like. I often use the Duplicator plugin for this purpose.
See: http://wordpress.org/plugins/duplicator/

Duplicator instruction
hidden link

#2
It would be better if you give me the test site rather than the live site.

#3
If the issues need some changes in the plugins updates/disabling we will install a plugin called “duplicator” and replicate the site in our local server for testing.

Look forward to your reply.

Thanks

May 24, 2018 at 9:19 am #2221082

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

I have fixed the issue on your site, could you please check it once?

The posts listed has two post status "publish" and "expired" so changed the code as follows to solve the issue.

function get_count_posts($post_type  ) {
     global $wpdb;
     $src_lang = isset( $_REQUEST['source_lang'] ) ? sanitize_text_field( $_REQUEST['source_lang'] ) : apply_filters( 'wpml_current_language', null );
     $lang = isset( $_REQUEST['lang'] ) ? sanitize_text_field( $_REQUEST['lang'] ) : $src_lang;
     $wpml_type = "post_".$post_type;
     $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} as p join wp_icl_translations as ict on p.id = ict.element_id  WHERE post_type = %s and ict.language_code = %s and ict.element_type = %s GROUP BY post_status";
 
     $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $post_type, $lang, $wpml_type ), ARRAY_A );
	foreach ( $results as $row ) {
         $counts[ $row['post_status'] ] = $row['num_posts'];
     }
     $result = (object) $counts;
 
     return $result->publish + $result->expired;
}

Let me know if this helps.

Thanks

May 24, 2018 at 10:57 am #2221867

Martynas.V

Hello Shekhar,

Thank you. It looks very good!

So you changed the code in the functions.php ? Was it the only thing?

May 25, 2018 at 4:14 am #2226739

Shekhar Bhandari
Supporter

Languages: English (English )

Timezone: Asia/Kathmandu (GMT+05:45)

Hello there,

Glad to know it worked,

Yes, the only changes I made was in funtions.php file.

Thanks