• Salve,
    con la seguente query ordino i post con due parametri la data e l’ordinamento
    Quello che praticamente vorrei ottenere è che a parità di data i post devono essere ordinati in maniera decrescente anche con il valore del campo ordinamento.
    Il problema è che word press considera la “parità di data” per tutti quei post che hanno giorno, mese, anno, ore, minuti e secondi uguali! Io invece ho bisogno di ordinare considerando solo giorno/mese/anno. Qualche suggerimento?

    $args = array(
    		'meta_query' => array(
    		'relation' => 'AND',
    		'field1' => array(
    			'key'     => 'ordinamento',
    			'compare' => 'EXISTS'
    		),
    		'field2' => array(
    			'key'     => 'visibile',
    			'value'   => '1',
    			'compare' => '='
    		),
    			'field3' => array(
    			'key'     => 'visibile_in_home',
    			'value'   => '1',
    			'compare' => '='
    		),
    	),
    	
    );
    	$query->set('meta_query', $args);
    	$query->set( 'orderby', array(
              'post_date' => 'DESC',
              'ordinamento' => 'DESC'));
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You would not be able to use that sort of ordering with WP_Query alone, the ORDER BY clause of the SQL would need modification. You can use the “posts_orderby” filter to do so.

    The post date time stamp needs to be divided by 86400 (sec in a day) to get an integer day count since epoch, something like UNIX_TIMESTAMP(post_date)/86400

    Thread Starter maxcondor

    (@maxcondor)

    ok grazie, ci provo.
    Grazie

    • This reply was modified 4 years, 1 month ago by maxcondor.
    Thread Starter maxcondor

    (@maxcondor)

    I modified the code hoping to get it as a date-only sort but using posts_orderby. However, I do not get the desired result

    	
    add_action('elementor/query/q_test_primopiano', function($query){
    $args = array(
    		'meta_query' => array(
    		'relation' => 'AND',
    		'field1' => array(
    			'key'     => 'ordinamento',
    			'compare' => 'EXISTS'
    		),
    		'field2' => array(
    			'key'     => 'visibile',
    			'value'   => '1',
    			'compare' => '='
    		),
    			'field3' => array(
    			'key'     => 'visibile_in_home',
    			'value'   => '1',
    			'compare' => '='
    		),
    			
    	),
    );
    	$query->set('meta_query', $args);
    	add_filter('posts_orderby','modifica_ordine',$query);
    	function modifica_ordine($ordine_personalizzato) {
    		$ordine_personalizzato= "post_date DESC";
    	}
    });
    • This reply was modified 4 years, 1 month ago by maxcondor.
    Moderator bcworkz

    (@bcworkz)

    Nesting a function declaration inside another makes it out of scope for the apply_filters() code trying to call it as a callback. Move the function declaration outside of the closure for ‘elementor/query/q_test_primopiano’.

    Ordering simply by post_date DESC will not use any meta value as secondary ordering. There will not be any identical post dates unless the date is converted to a timestamp and is integer divided by a large enough number.

    Thread Starter maxcondor

    (@maxcondor)

    thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘order by date’ is closed to new replies.