Sto provando a mettere in ordine i post degli archivi del mio sito in Wordpress per custom field e per farlo ho aggiunto un'action al mio file functions
Codice PHP:
add_action( 'pre_get_posts', 'my_change_sort_order');
function my_change_sort_order($query){
if(is_archive()) {
if( is_admin() )
{
return $query;
}
$meta_query = array( 'relation' => 'OR',
array( //check meta_key exist
'key' => 'my_custom_field',
'compare' => '!=',
'value' => ''
),
array( //if no meta_key exist
'key' => 'my_custom_field',
'compare' => 'NOT EXISTS'
));
$query->set( 'meta_query', $meta_query );
$query->set('meta_key', 'my_custom_field');
$query->set('orderby', 'meta_value_num');
$query->set('order', 'DESC');
return $query;
}
};
I problemi che sorgono sono questi:
- Mostra solo i post che hanno questo custom field, a me interessa che mostri tutti i post ma prima quelli che hanno questo campo = 1;
- Non mostra i thumbnails delle post.
Ho provato anche con
Codice PHP:
$meta_query = array(
'relation' => 'OR',
array( //check meta_key exist
'key' => 'my_custom_field',
'compare' => 'EXISTS'
),
array( //if no meta_key exist
'key' => 'my_custom_field',
'compare' => 'NOT EXISTS'
));
$query->set( 'meta_query', $meta_query );
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'my_custom_field');
$query->set('order', 'DESC');
Comunque il risultato è sempre quello: solo i post che hanno quel custom field e non mostra le immagini in evidenza.