Home › Forums › Front End PM PRO › Message Query by Meta Key
- This topic has 9 replies, 2 voices, and was last updated 2 years, 1 month ago by David.
-
AuthorPosts
-
November 1, 2022 at 2:40 pm #45042DavidParticipant
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.
November 1, 2022 at 3:09 pm #45045DavidParticipantSorry, 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 );
November 1, 2022 at 3:22 pm #45047DavidParticipantNo, 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.
November 1, 2022 at 9:01 pm #45054DavidParticipantI 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 );
November 1, 2022 at 9:45 pm #45056DavidParticipantOk, 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.
November 2, 2022 at 1:37 am #45058DavidParticipantOk, 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.
November 7, 2022 at 10:05 pm #45072Shamim HasanKeymasterPlease 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.
November 7, 2022 at 11:12 pm #45079DavidParticipantHi 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.
November 10, 2022 at 10:22 am #45088Shamim HasanKeymasterCurrently it supports order by only messages fields. You can use
fep_filter_message_query_sql
to change order by directly.November 10, 2022 at 1:50 pm #45094DavidParticipantOk, thanks very much for your help.
-
AuthorPosts
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.