NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

related-items.php 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. function grab_ids_related_to_and_from($id_to_remove, $p2p_res) {
  3. $related_to_ids = array_map(function($post) {
  4. return (int)$post->p2p_to;
  5. }, $p2p_res);
  6. $related_from_ids = array_map(function($post) {
  7. return (int)$post->p2p_from;
  8. }, $p2p_res);
  9. $unfiltered = array_merge($related_to_ids, $related_from_ids);
  10. $deduped = array_unique($unfiltered);
  11. return array_diff( $deduped, array($id_to_remove) );
  12. }
  13. function prepare_related_items($post_ids) {
  14. // Rearrange what fields get shown
  15. $collection = array();
  16. // Use IDs to get posts from the wpdb
  17. $args = [
  18. 'numberposts' => -1,
  19. 'post_type' => 'any',
  20. 'include' => $post_ids
  21. ];
  22. $related_items = get_posts($args);
  23. forEach( $related_items as $item ) {
  24. $collection[$item->ID] = default_post_format($item, false);
  25. }
  26. return $collection;
  27. }
  28. function p2p_related_to($id, $type) {
  29. global $wpdb;
  30. $sql = $wpdb->prepare(
  31. "SELECT * FROM wp_p2p
  32. WHERE p2p_from = %d
  33. OR p2p_to = %d",
  34. $id, $id
  35. );
  36. $res = $wpdb->get_results($sql);
  37. $related_posts_ids = grab_ids_related_to_and_from($id, $res);
  38. $related_posts = prepare_related_items($related_posts_ids);
  39. if(empty($related_posts_ids)) {
  40. return [];
  41. } else {
  42. return $related_posts;
  43. }
  44. }
  45. ?>