/** * Get a list of posts connected to other posts connected to a post. * * @param mixed $item An object, an object id or an array of such. * @param array $extra_qv Additional query variables to use. * * @return bool|object False on failure; A WP_Query instance on success. */ public function get_related($item, $extra_qv = array(), $output = 'raw') { $extra_qv['fields'] = 'ids'; $connected = $this->get_connected($item, $extra_qv, 'abstract'); $additional_qv = array('p2p:exclude' => _p2p_normalize($item)); return $this->flip_direction()->get_connected($connected->items, $additional_qv, $output); }
private function do_other_query($directed, $which) { $side = $directed->get($which, 'side'); $qv = array_merge($this->query, array('fields' => 'ids', 'p2p:per_page' => -1)); if ('any' != $this->items) { $qv['p2p:include'] = _p2p_normalize($this->items); } $qv = $directed->get_final_qv($qv, $which); return $side->capture_query($qv); }
/** * Create a connection. * * @param int $p2p_type A valid connection type. * @param array $args Connection information. * * @return bool|int False on failure, p2p_id on success. */ function p2p_create_connection($p2p_type, $args) { global $wpdb; extract(wp_parse_args($args, array('direction' => 'from', 'from' => false, 'to' => false, 'meta' => array())), EXTR_SKIP); list($from) = _p2p_normalize($from); list($to) = _p2p_normalize($to); if (!$from || !$to) { return false; } $args = array($from, $to); if ('to' == $direction) { $args = array_reverse($args); } $wpdb->insert($wpdb->p2p, array('p2p_type' => $p2p_type, 'p2p_from' => $args[0], 'p2p_to' => $args[1])); $p2p_id = $wpdb->insert_id; foreach ($meta as $key => $value) { p2p_add_meta($p2p_id, $key, $value); } do_action('p2p_created_connection', $p2p_id); return $p2p_id; }
static function alter_clauses($clauses, $q, $main_id_column) { global $wpdb; $clauses['fields'] .= ", {$wpdb->p2p}.*"; $clauses['join'] .= " INNER JOIN {$wpdb->p2p}"; // Handle main query if ('any' == $q['items']) { $search = false; } else { $search = implode(',', array_map('absint', _p2p_normalize($q['items']))); } $where_parts = array(); foreach ($q['p2p_type'] as $p2p_type => $direction) { if (0 === $p2p_type) { // used by migration script $part = "1 = 1"; } else { $part = $wpdb->prepare("{$wpdb->p2p}.p2p_type = %s", $p2p_type); } $fields = array('p2p_from', 'p2p_to'); switch ($direction) { case 'from': $fields = array_reverse($fields); // fallthrough // fallthrough case 'to': list($from, $to) = $fields; $part .= " AND {$main_id_column} = {$wpdb->p2p}.{$from}"; if ($search) { $part .= " AND {$wpdb->p2p}.{$to} IN ({$search})"; } break; default: if ($search) { $part .= " AND (\n\t\t\t\t\t\t({$main_id_column} = {$wpdb->p2p}.p2p_to AND {$wpdb->p2p}.p2p_from IN ({$search})) OR\n\t\t\t\t\t\t({$main_id_column} = {$wpdb->p2p}.p2p_from AND {$wpdb->p2p}.p2p_to IN ({$search}))\n\t\t\t\t\t)"; } else { $part .= " AND ({$main_id_column} = {$wpdb->p2p}.p2p_to OR {$main_id_column} = {$wpdb->p2p}.p2p_from)"; } } $where_parts[] = '(' . $part . ')'; } if (1 == count($where_parts)) { $clauses['where'] .= " AND " . $where_parts[0]; } else { $clauses['where'] .= " AND (" . implode(' OR ', $where_parts) . ")"; } // Handle custom fields if (!empty($q['meta'])) { $meta_clauses = _p2p_meta_sql_helper($q['meta']); foreach ($meta_clauses as $key => $value) { $clauses[$key] .= $value; } } // Handle ordering if ($q['orderby']) { $clauses['join'] .= $wpdb->prepare("\n\t\t\t\tLEFT JOIN {$wpdb->p2pmeta} AS p2pm_order ON (\n\t\t\t\t\t{$wpdb->p2p}.p2p_id = p2pm_order.p2p_id AND p2pm_order.meta_key = %s\n\t\t\t\t)\n\t\t\t", $q['orderby']); $order = 'DESC' == strtoupper($q['order']) ? 'DESC' : 'ASC'; $field = 'meta_value'; if ($q['order_num']) { $field .= '+0'; } $clauses['orderby'] = "p2pm_order.{$field} {$order}"; } return $clauses; }