Posts 2 Posts: query connected — orderby problem

So, now I’ve decided to sort in php: function compare_collections( $c1, $c2 ) { if( $c1->menu_order == $c2->menu_order ) { return ( $c1->post_title < $c2->post_title ) ? -1 : 1; } else { return ( $c1->menu_order < $c2->menu_order ) ? -1 : 1; } } // and then in my template: uasort( $colls->posts, ‘compare_collections’ ) … Read more

Function returning queried meta value based on current post ID

You can still do the AS with $wpdb, I find it still makes stuff look cleaner. You can use the $wpdb->prepare() method for sanitation. Are you looking for something like this? function combined_downloads($post_id) { global $wpdb; return $wpdb->get_var($wpdb->prepare( “SELECT SUM( meta_value ) FROM $wpdb->postmeta AS m LEFT JOIN $wpdb->posts AS p ON m.post_id = p.ID … Read more

Plugin Post 2 Posts: How to list most connected posts?

I have done this way: // Find connected pages (for all posts) p2p_type( ‘actors_movies’ )->each_connected( $wp_query ); Now we have a $connected property attached to every post on the $wp_query, that is an array of posts connected by actors_movies. $actors = array(); while (have_posts()) : the_post(); $movies_acted = count($post->connected); if($movies_acted > 0) { $actors[$post->ID] = … Read more

Creating connections programmatically with common fields on CPT’s

No need to code for this one as it was already solved by mikemanger with the plugin Batch Posts 2 Posts for, as the name says, batch creation of relations using Posts 2 Posts plugin framework. As in their description: This plugin makes it easy to create relations using the Posts 2 Posts plugin. For … Read more

Posts 2 Posts plugin: best way to change connection field value?

So, in my case this way hepled: 1) I’ve replaced ‘composer’ with ‘author’ in functions.php (in that part where connections are being registered) and 2) “edited” data in ${wpdb}_p2pmeta using phpmyadmin (UPDATE <yourprefix>_p2pmeta SET meta_value = “author” WHERE meta_value = “composer” or manually or like that). Please, backup your data, please test in any way … Read more

Adding a Custom Field to be used by Posts 2 Posts

Not sure if you really need to use the custom admin boxes, unless you need to customize labels, etc… <?php function my_connection_types() { // Make sure the Posts 2 Posts plugin is active. if ( !function_exists( ‘p2p_register_connection_type’ ) ) return; p2p_register_connection_type( array( ‘name’ => ‘cpt1_to_cpt2’, ‘from’ => ‘cpt1’, ‘to’ => ‘cpt2’, ‘admin_box’ => ‘from’, ‘fields’ … Read more