We're merging with GenerateBlocks! Learn more here.

[Resolved] order by custom field A and filter by custom field B

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support order by custom field A and filter by custom field B

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #35543
    Carlo
    Participant

    Hi 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

    #35562
    elvin
    Moderator

    Hi 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 the meta_key to your preference.

    Note: make sure the More settings tab is set to default and do your filtering on the shortcode completely.

    #35647
    Carlo
    Participant

    Hi 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

    #35694
    elvin
    Moderator

    In 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 slug place and value online 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);
    #35766
    Carlo
    Participant

    Thanks 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.

    #35779
    elvin
    Moderator

    Here’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);
    #35799
    Carlo
    Participant

    Great, thank you Elvin.

    #35854
    elvin
    Moderator

    No problem. 🙂

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