wordpresswpml

how to get all pages in WordPress when WPML plugin is activated?


I am developing WordPress site and in that I need a drop-down where I can load all list of pages available.

Here is the code that does it for me

$args = array(
    'sort_order' => 'ASC',
    'sort_column' => 'post_title',
    'post_type' => 'page',
    'post_status' => 'publish',
);
$pages = get_pages($args);

But when WPML plugin is activated and I have pages in different languages too. so above code only returns list of pages for one language either English or Italian.

I tired to find a way to search in the tickets of WPML forum but they all are deprecated functions and now need a solution to that. I tired with https://wpml.org/forums/topic/show-all-pages-wp_list_pages/ url but no luck.

Any good way how can I fix that ?

Any information on this would be greatly appreciated. Thanks!


Solution

  • After more research I found a proper solution. Here is the code that fetch all the pages by activate languages.

    $all_pages = array();
    
    $languages = apply_filters( 'wpml_active_languages', NULL, array( 'skip_missing' => 0));
    
    foreach( (array) $languages as $lang ) {
        /* change language */
        do_action( 'wpml_switch_language', $lang['code'] );
        /* building query */
        $posts = new WP_Query( array(
            'sort_order' => 'ASC',
            'sort_column' => 'post_title',
            'post_type' => 'page',
            'posts_per_page' => -1,
            'post_status' => 'publish',
        ) );
        $posts = $posts->posts;
        foreach( (array) $posts as $post ) {
            $all_pages[esc_url(get_page_link($post->ID))] = $post->post_title;
        }
    }