Hi Manhy,
Are you aware that you overwrite single variable ($idLangs) again and again in your for loop? This way, print_r($idLangs); prints only ID of translation in the very last language that is returned by $polylang->get_languages_list().
Also, it would help if you add some context to your code. Where is it placed? From where is it executed: global scope or a function?
Greets,
Česlav
Oops : sorry for the mistake, it’s an array :
$idLangs[] = pll_get_post….
I’m not good neither in php neither in wordpress, but here is what i’m doing :
I got several customs fields made with ACF and declared in a mu-plugin.
It works.
I would like to put in an array ids of each page of the site and dynamically locate the custom fields with the array values.
Actual ACF location :
`’location’ => array (
array (
array (
‘param’ => ‘page’,
‘operator’ => ‘==’,
‘value’ => ‘1696’,
),
),
array (
array (
‘param’ => ‘page’,
‘operator’ => ‘==’,
‘value’ => ‘1762’,
),
),
),’
I want to do this dinamically.
And I need to get the ids of all the translations of one page, but I can’t :
it seems that polylang functions are not declared when my mu-plugin is set.
So I tried to make a function to get ids in functions.php with an add_action called after mu plugin :
add_action( ‘muplugins_loaded’, ‘my_function’ )
but this didn’t work 🙁
And thanks for the answer of course ^^
Do you have Polylang installed as must-use plugin as well? If not, you should hook my_function to plugins_loaded action instead of muplugins_loaded.
Also, I recommend (whenever possible) to use functions that are part of Polylang API – this way you’re less likely to find out that your code does not work after you update Polylang in the future.
Try the following code:
function my_function() {
global $idLangs;
// Sanity check
if ( !function_exists('pll_languages_list') ) { return; }
$myFrenchId = 1696;
foreach ( pll_languages_list() as $slug ) {
$idLangs[] = pll_get_post($myFrenchId, $slug);
}
}
add_action( 'plugins_loaded', 'my_function' );
Hmm… I must be very silly but :
In my mu plugin (polylang isn’t installed as must-use) :
global $idLangs;
function my_function() {
// Sanity check
if ( !function_exists('pll_languages_list') ) { return; }
echo 'yes'; <-it prints
$myFrenchId = 1696;
foreach ( pll_languages_list() as $slug ) {
$idLangs[] = pll_get_post($myFrenchId, $slug);
}
print_r($idLangs);<-it prints
return $idLangs;
}
add_action( 'plugins_loaded', 'my_function' );
echo 'resultat:';
print_r($idLangs);
<-it doesn’t print
How can I get back the array out of my function, cause I can’t put all the creation’s code of custom fields in that function o_O
The last <-it doesn’t print is for the print_r outside of course
Bouaaaah ! just a $toto = myfunction() of course !
Merveilleux !!!!!!!!
Thank you so Chesio !
Oops, no : cache fake : I can’t get back my array out of my function…
It seems the problem you have is that you want to fetch data from Polylang before it is loaded. Must-use plugins are loaded before normal plugins, so if you call my_function from must-use plugin code, you will not get any results.
One solution is to move your custom fields declaration from must-use plugin to normal plugin. Even then I recommend to wrap the code that runs my_function in function hooked to plugins_loaded, because you have no way to determine in which order WordPress loads your plugins.
Yet another solution is to install Polylang as must-use plugin, but I have no experience with that and don’t know if Polylang works that way.
I would suggest to try something like:
function my_function() {
// as above
}
function my_custom_fields() {
// Get translated IDs.
$idLangs = my_function();
// Define your custom fields - you can use data from $idLangs here...
}
add_action('plugins_loaded', 'my_custom_fields');
This code works the same way if included in must-use or normal plugin.
Ok. I’ve understood.
Gonna sleep, and test all of your recommendations tomorrow morning.
Thanks u very much for help ^^
Well besides that I fought hard with multidimensionnal php arrays , I can now dynamically assign my ACF custom fields in each Polylang language, thanks to you. So again : thank you very much.