• Resolved bekee

    (@bekee)


    Hello! This is a really great plugin!

    I’m trying to set up a custom sort with FacetWP and am not sure how to get the correct Query data for “Most Helpful”. It looks like I’ll need to get the count of Pro by each Post ID but I’m not sure how to do that in the query args.

    Thanks for any help you can offer!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Pixelbart

    (@pixelbart)

    Hi @bekee,

    I’ve added extra helpers so that you can output the values with the help of post_id.

    Here is the link to Github, with the corresponding functions: https://github.com/pixelbart/helpful/blob/master/core/values.php

    // get pro for single post
    helpful_get_pro( $post_id = null );
    
    // get contra for single post
    helpful_get_contra( $post_id = null );
    

    Helpful also stores something in the meta fields, but not everything. Helpful’s meta fields are updated every time the overview pages are called in wp-admin.

    // get pro for single post
    get_post_meta( $post_id, 'helpful-pro', true);
    
    // get contra for single post
    get_post_meta( $post_id, 'helpful-contra', true);
    

    Hopefully that could help you a little.

    • This reply was modified 5 years, 5 months ago by Pixelbart.
    • This reply was modified 5 years, 5 months ago by Pixelbart.
    • This reply was modified 5 years, 5 months ago by Pixelbart.
    • This reply was modified 5 years, 5 months ago by Pixelbart.
    • This reply was modified 5 years, 5 months ago by Pixelbart.
    Plugin Author Pixelbart

    (@pixelbart)

    @bekee

    $args = [
    	'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;
    }
    $args = [
    	'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__( 'Contra:' ) );
    
    	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;
    }

    I’ve already tested the code on myself. All you have to do is insert the post_type as argument and change the order accordingly (ASC/DESC).

    • This reply was modified 5 years, 5 months ago by Pixelbart.
    • This reply was modified 5 years, 5 months ago by Pixelbart.
    Thread Starter bekee

    (@bekee)

    @pixelbart

    Wow! Thank you so much for your speedy reply. I appreciate it!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘FacetWP Sort By?’ is closed to new replies.