Skip Navigation

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.

This topic contains 1 reply, has 2 voices.

Last updated by Yvette 4 months, 1 week ago.

Assigned support staff: Yvette.

Author Posts
May 6, 2020 at 9:15 am #6068359

lorenzB-2

Hello,

Im numbering my custom post type "Playlists" with the function below.

It was working while my site was only in french. However, it is not working anymore because it is quering all posts and not only the one in my main language (fr). $count is jumping 2 numbers when there is a translation so numbering is now erratic.

How could I Improve this to $count++ only for the post in the main language (fr) ?

Thanks for your help and suggestions

function updateNumbers() {
    /* numbering the published posts, starting with 1 for oldest;
    / creates and updates custom field 'incr_number';
    / to show in post (within the loop) use <?php echo get_post_meta($post->ID,'incr_number',true); ?>
    / alchymyth 2010 */
    global $wpdb;
    $querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'playlist' ";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
    $counts = 0 ;
    if ($pageposts):
        foreach ($pageposts as $post):
            $counts++;
            add_post_meta($post->ID, 'incr_number', $counts, true);
            update_post_meta($post->ID, 'incr_number', $counts);
        endforeach;
   endif;
}
   
May 10, 2020 at 6:35 pm #6099211

Yvette
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/Madrid (GMT+02:00)

Hello

Our support for custom coding is limited. But would it help to be able to just get the posts in one language?

In this case, you could try using a WP_Query

include the

'suppress_filters' => false 

argument.

Here´s an example:
https://wpml.org/forums/topic/understanding-wp_query-translation/#post-2771007

And here is another approach using the pre_get_posts hook
https://wpml.org/forums/topic/display-custom-posts-from-any-language/#post-1939060

The important thing is to set the supress_filters query argument to false and use the nomal WP query funcions that our filters hook into.

Does this help?

The topic ‘[Closed] Get only main language SELECT query’ is closed to new replies.