Documentation

Didn't find anything? Ask your questions in the forum.
Documentation Professionals Query posts with order by meta value helpful

Query posts with order by meta value helpful

As this was recently asked for, I would like to put down the solution here as well, should there be further questions of this kind in the future. Here you can see how to output the values for Helpful with WP_Query and how to sort them by them.

Helpful actually stores the values in the database and compares the post meta with the database only when you call the overview page in WordPress Admin.

Pro

/**
 * Order posts by helpful pro in descendend order.
 */
$args = [
	'post_type'      => 'post',
	'posts_per_page' => -1,
	'meta_query'     => [],
	'fields'         => 'ids',
	'meta_key'       => 'helpful-pro',
	'orderby'        => 'meta_value_num',
	'order'          => 'DESC',
];

$query = new WP_Query( $args );

if ( $query->found_posts ) {

	printf( '<strong>%s</strong><br>', esc_html__( 'Pro:' ) );

	foreach ( $query->posts as $post_id ) :
		printf( '%s: %d<br>', get_the_title( $post_id ), (int) get_post_meta( $post_id, 'helpful-pro', true ) );
	endforeach;
}

Contra

/**
 * Order posts by helpful contra in descendend order.
 */
$args = [
	'post_type'      => 'post',
	'posts_per_page' => -1,
	'meta_query'     => [],
	'fields'         => 'ids',
	'meta_key'       => 'helpful-contra',
	'orderby'        => 'meta_value_num',
	'order'          => 'DESC',
];

$query = new WP_Query( $args );

if ( $query->found_posts ) {

	printf( '<strong>%s</strong><br>', esc_html__( 'Pro:' ) );

	foreach ( $query->posts as $post_id ) :
		printf( '%s: %d<br>', get_the_title( $post_id ), (int) get_post_meta( $post_id, 'helpful-contra', true ) );
	endforeach;
}

Was this post helpful?