Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author dFactory

    (@dfactory)

    @beee, your code looks ok. Please try playing with action priority:

    add_action( 'pre_get_posts', 'alter_query_for_faq', 20 );

    or

    add_action( 'pre_get_posts', 'alter_query_for_faq', 0 );

    Thread Starter Beee

    (@beee)

    Yeah the code works perfectly except the orderby doesn’t seem to work.
    Adding several priorities between 0 and 9999 didn’t change a thing.

    I just read this on the get_posts explanation:

    Also note that even if ‘suppress_filters’ is true, any filters attached to pre_get_posts are still applied—only filters attached on ‘posts_*’ or ‘comment_feed_*’ are suppressed.

    Could this be related ? Not 100% sure if it’s related.

    Plugin Author dFactory

    (@dfactory)

    This is related – “suppress_filters” has to be set to false (for example we force that in our pvc_get_most_viewed_posts() function, because get_posts() sets “suppress_filters” to true by default.);

    What if you try to set the orderby query parameter in parse_query action (it’s very similar to pre_get_posts but gets fired earlier)

    Thread Starter Beee

    (@beee)

    i used this… all i got was one result…

    function alter_query_for_faq_pq($query) {
        if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'faq' ) ) {
            $query->query_vars['posts_per_page'] = '20';
            if ( class_exists( 'Post_Views_Counter' ) ) {
                $query->query_vars['orderby'] = 'post_views';
                $query->query_vars['order'] = 'DESC';
                $query->query_vars['suppress_filters'] = false;
            } else {
                $query->query_vars['orderby'] = 'rand';
            }
        }
    }
    add_action('parse_query', 'alter_query_for_faq_pq');

    Plugin Author dFactory

    (@dfactory)

    maybe try settting orderby and optionally suppress_filters in that action, but keep the rest in pre get posts.

    Thread Starter Beee

    (@beee)

    did that but same as above, 1 result only…

    it appears the culprit is this line

    $query->query_vars['orderby'] = 'post_views';

    because if i remove it i get the results I want, just not ordered the wayI want…

    Plugin Author dFactory

    (@dfactory)

    Ok, contact us via our page – we’ll send you a beta version.

    Maybe that fixes the issue – if not, we’ll try to do some more testing.

    Thread Starter Beee

    (@beee)

    done

    Thread Starter Beee

    (@beee)

    btw where are the views stored ?

    Thread Starter Beee

    (@beee)

    the beta didn’t fix anything, neither does the new version.

    Thread Starter Beee

    (@beee)

    i managed to fix it but not sure if it’s because of the new version or my altered code.

    This is what I use now… everything in parse_query instead of pre_get_posts.

    function alter_query_for_faq($query) {
        if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'faq' ) ) {
            if ( class_exists( 'Post_Views_Counter' ) ) {
                $query->query_vars['orderby'] = 'post_views';
                $query->query_vars['order'] = 'DESC';
                $query->query_vars['posts_per_page'] = '20';
                $query->query_vars['suppress_filters'] = false;
            }
        }
    }
    add_action('parse_query', 'alter_query_for_faq', 99);

    I’m just happy it works the way I want.

    Plugin Author dFactory

    (@dfactory)

    Maybe the suppress_filters did the trick.

    But that should be like that by default.

    Thread Starter Beee

    (@beee)

    who knows ?? i’m happy with the result.

    but where are the views stored ?

    Plugin Author dFactory

    (@dfactory)

    in a separate database table (basically for performance reasons, especially on large sites).

    but we’ve provided a set of native WP_Query extensions to make use of it just like with normal query parameters.

    Thread Starter Beee

    (@beee)

    where can i find those ?

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘orderby post_views is not 'recognized' in pre_get_posts’ is closed to new replies.