Home › Forums › Pro Support › order by custom field A and filter by custom field B
Tagged: custom fields, filter, order by
- This topic has 7 replies, 2 voices, and was last updated 1 year, 10 months ago by
elvin.
-
AuthorPosts
-
November 16, 2021 at 12:43 pm #35543
Carlo
ParticipantHi there,
time ago I have created this shortcode in functions.php with the purpose of order the list by the “idorder” custom field
add_shortcode('wpsp_dinamico_idorder', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '' ), $atts, 'dynamic_wpsp' ); $settings = array( 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'idorder', ); ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
the shortcode works fine.
Now I need to apply the same shortcode to a list that has a filter set in wpshowposts “more settings” tab. The filter is the string “place” in the field “Meta Key” with the “Meta value” field set to the string “online”:
The list works fine without the shortcode. But when I add the shortcode, the list is not working anymore and shows the output “Sorry, no posts were found.”
The same list works fine with the shortcode but with no filter applied. So, I guess there is some conflict in the code.
How can I fix it? Thank you. All the best. Carlo
November 16, 2021 at 5:39 pm #35562elvin
ModeratorHi Carlo,
That exact code will be in conflict with the list with More settings fields filled up.
If you want to modify the meta key on the go as well then you should write the shortcode atts to allow for it.
Example:
add_shortcode('wpsp_dinamico_idorder', function($atts){ $queriedArchive = get_queried_object(); $atts = shortcode_atts( array( 'id' => '', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'idorder', ), $atts, 'wpsp_dinamico_idorder' ); $settings = array( 'order' => $atts['order'], 'orderby' => $atts['orderby'], 'meta_key' => $atts['meta_key'], ); ob_start(); wpsp_display( $atts['id'], $settings ); return ob_get_clean(); });
With this, we can do more with the shortocde.
Example:
[wpsp_dinamico_idorder id="123" order="ASC" orderby="meta_value" meta_key="place"]
You can replace themeta_key
to your preference.Note: make sure the More settings tab is set to default and do your filtering on the shortcode completely.
November 20, 2021 at 3:43 am #35647Carlo
ParticipantHi Elvin,
I am not sure if I haven’t got it or if the code is covering my purposes partially:
I need to apply a filter by a custom field “Place” with the value “online” (in order to select only the posts that have the value “online” in that custom field) AND set the order by another custom field whose name is “idorder”.
Can I do it with the code you provided? If yes, would you mind applying it to the shortcode?Thank you. Best wishes. Carlo
November 21, 2021 at 4:55 pm #35694elvin
ModeratorIn that case you may have to use WPSP’s base default shortcode and do the filtering through a filter hook instead.
To run the code properly, you’ll have to modify your plugin as Tom instructed here – https://wpshowposts.com/support/topic/can-i-exclude-with-a-custom-meta-field-from-a-list/#post-13344
Example: Say, you want to filter WPSP list id
1234
with custom field slugplace
and valueonline
while ordering it using idorder field.add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) { if (1234 === (int) $settings["list_id"]) { $args['meta_key'] = 'idorder'; $args['orderby'] = 'meta_value_num'; $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'idorder', ), array( 'key' => 'place', 'value' => 'online', 'compare' => 'LIKE' ), ); } return $args; },10,2);
November 25, 2021 at 1:58 am #35766Carlo
ParticipantThanks Elvin, it works now.
Could you post an example of 2 lists?
Something like:
if list 1111 then (order x and filter y)
if list 2222 then (order z and filter w)Thank you.
November 25, 2021 at 7:47 pm #35779elvin
ModeratorHere’s an example for that.
add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) { if (1111 === (int) $settings["list_id"]) { $args['meta_key'] = 'x'; $args['orderby'] = 'meta_value_num'; $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'x', ), array( 'key' => 'y', 'value' => 'y value', 'compare' => 'LIKE' ), ); } if (2222 === (int) $settings["list_id"]) { $args['meta_key'] = 'z'; $args['orderby'] = 'meta_value_num'; $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => 'z', ), array( 'key' => 'w', 'value' => 'w value', 'compare' => 'LIKE' ), ); } return $args; },10,2);
November 26, 2021 at 3:03 am #35799Carlo
ParticipantGreat, thank you Elvin.
November 28, 2021 at 4:07 pm #35854elvin
ModeratorNo problem. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.