Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #1291470

    Hello,
    I am looking to change the order of the posts using the blog posts element.

    I have 2 custom fields I can use either:
    newsdate (added manually)
    or
    xn-wppe-expiration (added by plugin to expire posts)

    I have added the following modified code to the functions.php on my child theme, tried the original posted cant find the link now but can add later if needed

    I’ve added the values 1,2,3,4,5 to the fields and selected the options added to the Blog posts section, I select Meta Value, News Date or Expiration Date and ascending or descending but it just reverts back to the post date as the sorting field, the values save properly as they stay when I go back in

    code added is:

    if(!function_exists(‘avia_custom_query_extension’))
    {
    function avia_custom_query_extension($query, $params)
    {
    global $avia_config;
    if(!empty($avia_config[‘avia_custom_query_options’][‘order’]))
    {
    $query[‘order’] = $avia_config[‘avia_custom_query_options’][‘order’];
    }

    if(!empty($avia_config[‘avia_custom_query_options’][‘orderby’]))
    {
    $query[‘orderby’] = $avia_config[‘avia_custom_query_options’][‘orderby’];
    }

    unset($avia_config[‘avia_custom_query_options’]);

    return $query;
    }

    add_filter(‘avia_masonry_entries_query’, ‘avia_custom_query_extension’, 10, 2);
    add_filter(‘avia_post_grid_query’, ‘avia_custom_query_extension’, 10, 2);
    add_filter(‘avia_post_slide_query’, ‘avia_custom_query_extension’, 10, 2);
    add_filter(‘avia_blog_post_query’, ‘avia_custom_query_extension’, 10, 2);
    add_filter(‘avf_magazine_entries_query’, ‘avia_custom_query_extension’, 10, 2);

    add_filter(‘avf_template_builder_shortcode_elements’,’avia_custom_query_options’, 10, 1);
    function avia_custom_query_options($elements)
    {
    $allowed_elements = array(‘av_blog’,’av_masonry_entries’,’av_postslider’,’av_portfolio’,’av_magazine’);

    if(isset($_POST[‘params’][‘allowed’]) && in_array($_POST[‘params’][‘allowed’], $allowed_elements))
    {
    $elements[] = array(
    “name” => __(“Custom Query Orderby”,’avia_framework’ ),
    “desc” => __(“Set a custom query orderby value”,’avia_framework’ ),
    “id” => “orderby”,
    “type” => “select”,
    “std” => “”,
    “subtype” => array(
    __(‘Default Order’, ‘avia_framework’ ) =>”,
    //__(‘Sort Date’, ‘avia_framework’ ) =>’xn-wppe-expiration’,
    //__(‘EventDate’, ‘avia_framework’ ) =>’newsdate’,
    __(‘Title’, ‘avia_framework’ ) =>’title’,
    __(‘Random’, ‘avia_framework’ ) =>’rand’,
    __(‘Date’, ‘avia_framework’ ) =>’date’,
    __(‘Author’, ‘avia_framework’ ) =>’author’,
    __(‘Name (Post Slug)’, ‘avia_framework’ ) =>’name’,
    __(‘Modified’, ‘avia_framework’ ) =>’modified’,
    __(‘Comment Count’, ‘avia_framework’ ) =>’comment_count’,
    __(‘Page Order’, ‘avia_framework’ ) =>’menu_order’,
    __(‘Meta Value’, ‘avia_framework’ ) =>’meta_value’,
    __(‘Meta Value Number’, ‘avia_framework’ ) =>’meta_value_num’)
    );

    $elements[] = array(
    “name” => __(“Custom Query Meta Key”,’avia_framework’ ),
    “desc” => __(“Set custom meta keys to order by”,’avia_framework’ ),
    “id” => “meta_key”,
    “type” => “select”,
    “std” => “”,
    “subtype” => array(
    __(‘None’, ‘avia_framework’ ) =>”,
    __(‘News Date’, ‘avia_framework’ ) =>’newsdate’,
    __(‘Expiration Date’, ‘avia_framework’ ) =>’xn-wppe-expiration’));

    $elements[] = array(
    “name” => __(“Custom Query Order”,’avia_framework’ ),
    “desc” => __(“Set a custom query order”,’avia_framework’ ),
    “id” => “order”,
    “type” => “select”,
    “std” => “”,
    “subtype” => array(
    __(‘Default Order’, ‘avia_framework’ ) =>”,
    __(‘Ascending Order’, ‘avia_framework’ ) =>’ASC’,
    __(‘Descending Order’, ‘avia_framework’ ) =>’DESC’));
    }

    return $elements;
    }

    add_filter(‘avf_template_builder_shortcode_meta’, ‘avia_custom_query_add_query_params_to_config’, 10, 4);
    function avia_custom_query_add_query_params_to_config($meta, $atts, $content, $shortcodename)
    {
    global $avia_config;
    if(empty($avia_config[‘avia_custom_query_options’])) $avia_config[‘avia_custom_query_options’] = array();

    if(!empty($atts[‘order’]))
    {
    $avia_config[‘avia_custom_query_options’][‘order’] = $atts[‘order’];
    }

    if(!empty($atts[‘orderby’]))
    {
    $avia_config[‘avia_custom_query_options’][‘orderby’] = $atts[‘orderby’];
    }

    return $meta;
    }
    }

    #1292395

    Hey marladesign,

    Thank you for the update.

    Instead of using the avia_custom_query_extension function, try to adjust the blog posts query directly using the avia_blog_post_query filter.

    Example:

    add_filter('avia_blog_post_query', 'avia_modify_post_grid_query_desc');
    function avia_modify_post_grid_query_desc( $query ) {
        $query['orderby'] = 'date';
        $query['order'] = 'DESC';
        return $query;
    }
    

    You will have to include the meta_value or the meta_value_num parameters, and define the meta_key accordingly.

    // https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

    Best regards,
    Ismael

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.