NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

related-items.php 1.3KB

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