• Resolved creativecatapps

    (@creativecatapps)


    Hi,

    I want be able to output a list of posts, similar to the WP Display Posts plugin:

    [display-posts orderby="meta_value_num" meta_key="shared_counts_total"]

    I’d rather not install another plugin but can’t see how I could implement this in a Query Loop.

    If I presume there is a standard meta_key for each post, with a numerical value, Is this possible?

    Thanks,
    Emma

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    Hi Emma,

    You can try adding to cu-query the class list of the Grid Block of the Query Loop Block.

    Then add this PHP snippet:

    add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
    		if ( ! is_admin() && ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-query' ) !== false ) {
    			$query_args['orderby'] = 'meta_value';
    			$query_args['meta_key'] = 'my_meta_name';
    		}
    
    		return $query_args;
    	}, 10, 2 );
    

    You can use a plugin like Code Snippets to add this code.

    Thread Starter creativecatapps

    (@creativecatapps)

    That works a treat – thank you!

    Is there a way to cache the results in a transient or similar, for performance, or is the impact negligible?

    Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    You’ll need to alter the plugin code to cache data through Transients.

    What you could try is Object Caching from a plugin instead.

    You’re welcome, @creativecatapps!

    Plugin Support fernandoazarcon2

    (@fernandoazarcon2)

    Hi there! We haven’t heard back from you for a while now so we’re going to go ahead and set this topic as resolved. Feel free to reply if you need any more help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Query Loop Order By Meta Key’ is closed to new replies.