Message Query by Meta Key


Home Forums Front End PM PRO Message Query by Meta Key

This topic is: Resolved
Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #45042
    David
    Participant

    Hi Shamim,

    I hope you are well.

    I am trying to filter messages by meta key when a certain URL parameter is present. I have tried the following code but it does not work. When I remove the $args['participant_query'][] = array('mgs_participant' => get_current_user_id(), it works ok, but it need to only show messages for that user. I am doing something wrong here?


    add_filter( 'fep_message_query_args', function( $args, $user_id ) {
    if (in_array('subscriber', wp_get_current_user()->roles) && isset($_GET['partotal'])) {
    $args['participant_query'][] = array(
    'mgs_participant' => get_current_user_id(),
    'meta_query' => array(
    array(
    'key' => 'parent_currency',
    'value' => 'LKR',
    'compare' => '=',
    ),
    ),
    'orderby' => 'meta_value',
    'order' => 'ASC'
    );
    return $args;
    } else {
    return $args;
    }
    }, 10, 2 );

    Thanks for any help you can offer.

    #45045
    David
    Participant

    Sorry, figured it out. Answer for anyone needing it in the future:


    add_filter( 'fep_message_query_args', function( $args, $user_id ) {
    if (in_array('subscriber', wp_get_current_user()->roles) && isset($_GET['partotal'])) {
    $args = array(
    'meta_query' => array(
    array(
    'key' => 'parent_currency',
    'value' => 'LKR',
    'compare' => '=',
    ),
    ),
    'orderby' => 'meta_value',
    'order' => 'DESC'
    );
    $args['participant_query'][] = array(
    'mgs_participant' => $user_id);
    return $args;
    } else {
    return $args;
    }
    }, 10, 2 );

    #45047
    David
    Participant

    No, thought I had figured it out but still not working. I just want to query the messagebox by a meta key and then orderby that meta key number. Any help is appreciated.

    #45054
    David
    Participant

    I have also tried this but I just cannot make it work.


    add_filter( 'fep_message_query_args', function( $args, $user_id ) {
    if (in_array('subscriber', wp_get_current_user()->roles) && isset($_GET['partotal'])) {
    unset( $args['orderby'], $args['meta_key'], $args['order'] );
    $args['meta_key'] = 'parent_total';
    $args['orderby'] = 'meta_value_num';
    $args['order'] = 'DESC';
    return $args;
    } else {
    return $args;
    }
    }, 10, 2 );

    #45056
    David
    Participant

    Ok, I think I have figured this out! I thought it was using the parent message which is the one with the meta_key but of course it is not the parent, it is the child message that is being queried and these do not have the meta keys. I will have to rethink this.

    #45058
    David
    Participant

    Ok, I have tried this and the meta_key is definitely there but the following code is not doing anything:


    add_filter( 'fep_message_query_args', 'totalsortjg');
    function totalsortjg( $args ) {
    if (in_array('subscriber', wp_get_current_user()->roles) && isset($_GET['partotal'])) {
    $args['meta_key'] = 'total_sort';
    }
    return $args;
    }

    I am expecting to only see messages with the meta_key present. Any help is appreciated.

    #45072
    shamim
    Keymaster

    Please try like following

    add_filter( 'fep_message_query_args', function( $args ){
        $args['meta_query'][] = [
            'key'     => 'parent_currency',
    	'value'   => 'LKR',
    	'compare' => '='
        ];
        return $args;
    });
    

    Let me know.

    #45079
    David
    Participant

    Hi Shamim,

    Thanks very much. The meta query is working but I am also trying to orderby the meta value. I have tried everything I can think of but it won’t work. This is what I have:


    add_filter( 'fep_message_query_args', 'totalsortjg');
    function totalsortjg( $args ) {
    if (in_array('subscriber', wp_get_current_user()->roles) && isset($_GET['partotal'])) {
    $args['meta_query'][] = ['totalsort' => [
    'key' => 'total_sort',
    'value' => 1,
    'compare' => '>',
    'type' => 'NUMERIC'
    ]];
    $args['orderby'] = ['totalsort' => 'DESC'];
    }
    return $args;
    }

    Thanks for the help.

    #45088
    shamim
    Keymaster

    Currently it supports order by only messages fields. You can use fep_filter_message_query_sql to change order by directly.

    #45094
    David
    Participant

    Ok, thanks very much for your help.

Viewing 10 posts - 1 through 10 (of 10 total)

You need to purchase ‘Front End PM PRO’ to create topic in this support forum.

If you already purchased ‘Front End PM PRO’ please LOGIN.