• Resolved sia77

    (@sia77)


    Hi Dcooney,

    I have the following shortcode: [ajax_load_more post_type="post, article, event, resource, service, team_member" order="ASC" orderby="meta_value_num date" exclude="673"]

    So what I’m trying to achieve here is to sort by meta_value_num, which is post_weight, and when the post_weight for each post is the same sort by date. So first the meta_value_num takes effect, then the date…

    Doing this, it doesn’t seem to be working properly, I’m seeing a newer post (March 2015) showing up below posts that were created in 2014, but have the same post_weight…

    Any ideas?

    Thanks,
    Sia77

    https://wordpress.org/plugins/ajax-load-more/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Darren Cooney

    (@dcooney)

    I believe in order to order by meta_value_num you need to also reference a meta_key in your query.

    Issue with this is that their is currently a wee bug in alm 2.7.0 that prevents querying by meta_key if meta_value is null.

    Plugin Author Darren Cooney

    (@dcooney)

    I just pushed 2.7.1 of ALM that may solve your issue.

    [ajax_load_more post_type="post, article, event, resource, service, team_member" order="ASC" meta_key="year" orderby="meta_value_num date" exclude="673"]

    Notice the meta_key="year" – you are required to enter a meta_key to orderby meta_value_num

    Thread Starter sia77

    (@sia77)

    Let me ask you a question. Is it possible to create a short code where you one can specify the following:

    sort meta_value_num in ascending order
    sort date in descending order

    Plugin Author Darren Cooney

    (@dcooney)

    In order to sort by meta_value_num I believe you need a meta_key. Otherwise the query doesnt know what meta_value_num is.

    Thread Starter sia77

    (@sia77)

    Hi DCooney,

    Here is what I have now, and it works fine, and sorts by meta_key.

    [ajax_load_more post_type="post" order="ASC" meta_key="post_weight" orderby="meta_value_num" exclude="673"]

    Here is the problem: I would like to be also able to sort by WP’s post/publish date at the same time as follows:

    1. Sorting by meta_values for post_weight (meta_key) takes precedence
    2. When the meta_values for post_weight are the same then sort by date in DESC order….

    Example:

    'meta_query' => array(
          array('key' =>'post_weight',
                'type' => 'numeric')
       ),array('meta_value_num'=>'ASC','date'=>'DESC'),

    Could you confirm if the plugin can handle that? If yes, could you let me know how to set-up the shortcode…

    Thread Starter sia77

    (@sia77)

    I just did this, and I think it worked:

    [ajax_load_more post_type=”post, article, event, resource, service, team_member” order=”ASC,DESC” meta_key=”post_weight” orderby=”meta_value_num,date” exclude=”673″]

    I still want to make sure this is not a happy coincident, so if you confirm, that would be appreciated.

    Thread Starter sia77

    (@sia77)

    Ok. So the solution to my problem to handle the following shortcode was:
    [ajax_load_more post_type="post, article, event, resource, service, team_member" order="ASC:DESC" meta_key="post_weight" orderby="meta_value_num:date" exclude="673"]

    I added the following to ajax-load-more.php

    if(!empty($orderby) && !empty($order) ){
    
                $orderbys = explode(":", $orderby);
                $orders = explode(":", $order);
    
                $args['orderby'] = alm_get_orderby_query($orderbys, $orders);
    
             }

    and I added the following to function.php of ajax load more plugin:

    function alm_get_orderby_query($orderbys, $orders){
    
    	$temp_arr = array();
    
    	for($i=0; $i<count($orderbys);$i++){
    		$temp_arr[$orderbys[$i]]= $orders[$i];
    	}
    
    	return $temp_arr;
    
    }

    Obviously this can be expanded to address a more general concern.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘order by meta value and date…’ is closed to new replies.