NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

query-post.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. class P2P_Query_Post {
  3. static function init() {
  4. add_action( 'parse_query', array( __CLASS__, 'parse_query' ), 20 );
  5. add_filter( 'posts_clauses', array( __CLASS__, 'posts_clauses' ), 20, 2 );
  6. add_filter( 'posts_request', array( __CLASS__, 'capture' ), 999, 2 );
  7. add_filter( 'the_posts', array( __CLASS__, 'cache_p2p_meta' ), 20, 2 );
  8. }
  9. static function parse_query( $wp_query ) {
  10. $r = P2P_Query::create_from_qv( $wp_query->query_vars, 'post' );
  11. if ( is_wp_error( $r ) ) {
  12. $wp_query->_p2p_error = $r;
  13. $wp_query->set( 'year', 2525 );
  14. return;
  15. }
  16. if ( null === $r )
  17. return;
  18. list( $wp_query->_p2p_query, $wp_query->query_vars ) = $r;
  19. $wp_query->is_home = false;
  20. $wp_query->is_archive = true;
  21. }
  22. static function posts_clauses( $clauses, $wp_query ) {
  23. global $wpdb;
  24. if ( !isset( $wp_query->_p2p_query ) )
  25. return $clauses;
  26. return $wp_query->_p2p_query->alter_clauses( $clauses, "$wpdb->posts.ID" );
  27. }
  28. static function capture( $request, $wp_query ) {
  29. global $wpdb;
  30. if ( !isset( $wp_query->_p2p_capture ) )
  31. return $request;
  32. $wp_query->_p2p_sql = $request;
  33. return '';
  34. }
  35. /**
  36. * Pre-populates the p2p meta cache to decrease the number of queries.
  37. */
  38. static function cache_p2p_meta( $the_posts, $wp_query ) {
  39. if ( isset( $wp_query->_p2p_query ) && !empty( $the_posts ) )
  40. update_meta_cache( 'p2p', wp_list_pluck( $the_posts, 'p2p_id' ) );
  41. return $the_posts;
  42. }
  43. }