E
E
Evgeny Fedorov2014-11-09 03:23:39
PHP
Evgeny Fedorov, 2014-11-09 03:23:39

How to write a cycle of posts in vp sorted by metakey with pegging?

Wrote a plugin like buttons for posts. It's just that when you click on the button, the meta key values ​​\u200b\u200band change the values ​​​​in the database in a separate table. Now the question is how to write a post output loop sorted by likes. That is, the most liked from the top?
The ideas that are in my head.
1 Loop through all the posts and create a meta key for them equal to -9999 by default and then write a loop for displaying posts with the rule that if like is -9999 then do not show this number at all. Like a simulation. But I didn’t find in vp how you can register a meta key for all posts at once, only SQL inserts.
2 I wrote two cycles. The first one displays all posts with a meta key (that have a given tag key) and sorted by meta key. In the second cycle, display all posts that do not have this meta key. Get what you need, except that these two cycles cannot be divided into pages with navigation at the bottom.

$args = array(
    'post_status' => 'publish',
    'post_type' => 'post',
    'meta_key' => 'vote-count',
    'orderby' => 'meta_value_num'
);
show_posts($args);
$args = array(
  'post_status' => 'publish',
    'post_type' => 'post',
    'posts_per_page' => -1,
    'meta_query' => array(
      array(
          'key' => 'vote-count',
          'compare' => 'NOT EXISTS'
      )
    )
);
show_posts($args);
function show_posts($args) {
  $query = new WP_Query($args);
  if ( $query->have_posts() ) {
    echo '<ul >';
    while ( $query->have_posts() ) {
      $query->the_post();
      echo '<li style="margin-top: 10px;">' .'<span style="color:red;">' . get_post_meta( get_the_ID(),'vote-count',true) . ' </span> '. get_the_title(). '</li>';
    }
    echo '</ul>';
  }
  wp_reset_query();
}

// 'relation' => 'OR',  - Для него не работает сортировка.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Fedorov, 2014-11-09
@JekFdrv

And so the update. The most official answer that other plugins use is this:

add_action( 'pre_get_posts', 'wpse_71899_start_filter' );

function wpse_71899_start_filter() {
  if ( ! is_front_page() )
        return; // stop here.
  add_filter('posts_orderby', 'edit_posts_orderby');
  add_filter('posts_join_paged','edit_posts_join_paged');
}
function edit_posts_join_paged($join_paged_statement) {
  global $wpdb;
  $join_paged_statement = "LEFT JOIN ".$wpdb->prefix."post_vote_counts ON ".$wpdb->prefix."post_vote_counts.post_id = $wpdb->posts.ID";
  return $join_paged_statement;	
}

function edit_posts_orderby($orderby_statement) {
  global $wpdb;
  $orderby_statement = "(".$wpdb->prefix."post_vote_counts.upvote_count) DESC";
  return $orderby_statement;
}

BUT, this code works only for the home page, and this cannot be changed in any way, if we remove the rule, the posts in the categories disappear, that is, in general, the categories will not display anything in a cycle. I don't even know how to use it. VP has a hole in it. In theory, you need to specify some kind of variable in the cycle that will switch and use this type of post output.

A
Alexander Zelenin, 2014-11-09
@zelenin

use the pre_get_posts action. Documentation in the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question