WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] List all posts connected to another single post when not viewing a post (10 posts)

  1. duck__boy
    Member
    Posted 1 year ago #

    Good morning,

    I'm trying to filter the Posts table in the admin to show only Posts that are connected to a certain Person. I have added a custom column to the table to show who is connected, with each name being a link.
    When the link is cliked, I am using the request filter to manipulate the query, but I cannot figure out what I need to do.

    I've tried using he ID of the Person for 'connected_items' but no results are returned, I've also tried not using 'connected_items', but that throws up an an error.

    For example - URL = http://test.dynedrewett.com/wp-admin/edit.php?post_type=post&contact=458, where contact=458 is the ID of the person whose connections I want to show.

    function filter_custom_columns($vars){
    
    	if(isset($_GET['contact']) &&  $_GET['contact'] !== '') :
    
    		$new_vars['connected_type'] = 'news_to_people';
    		$new_vars['connected_items' ] = $_GET['contact'];
    		$new_vars['direction'] = 'from';
    
    	endif;
    
    	return $vars + $new_vars;
    
    }
    add_filter('request', 'filter_custom_columns');

    Here is the regestered connection -

    p2p_register_connection_type(array(
    		'name' => 'news_to_people',
    		'from' => POST_TYPE_NEWS,
    		'to' => POST_TYPE_STAFF,
    		'title' => array('from' => 'Staff linked to this Story', 'to' => 'Stories that this Staff Member is linked to')
    	));

    Any help here would be appricated. Thanks.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    That's all done for you, if you use the 'admin_column' option:

    https://github.com/scribu/wp-posts-to-posts/wiki/Admin-column-display

  3. duck__boy
    Member
    Posted 1 year ago #

    Thanks for the quick reply, but unfortunately it's not working. I'm getting the extra admin column, but clicking the link returns no results.

    Here is an example of the link (which I'm sure is as it should be) -

    http://test.dynedrewett.com/wp-admin/edit.php?connected_type=news_to_people&connected_direction=to&connected_items=458&post_type=post

    Any ideas at all? Thanks.

    FYI, here is the full query that is created -

    SELECT SQL_CALC_FOUND_ROWS wp_posts.*, wp_p2p.*
    FROM wp_posts
    INNER JOIN wp_p2p
    WHERE 1=1
    AND wp_posts.post_type IN ('post', 'tla')
    AND (
    	wp_posts.post_status = 'publish'
    	OR wp_posts.post_status = 'future'
    	OR wp_posts.post_status = 'draft'
    	OR wp_posts.post_status = 'pending'
    	OR wp_posts.post_status = 'private'
    )
    AND (
    	wp_p2p.p2p_type = 'news_to_people'
    	AND wp_posts.ID = wp_p2p.p2p_from
    	AND wp_p2p.p2p_to IN (
    		SELECT wp_posts.ID
    		FROM wp_posts
    		WHERE 1=1
    		AND wp_posts.ID IN (458)
    		AND wp_posts.post_type IN ('post', 'tla')
    		AND (
    			wp_posts.post_status = 'publish'
    			OR wp_posts.post_status = 'future'
    			OR wp_posts.post_status = 'draft'
    			OR wp_posts.post_status = 'pending'
    			OR wp_posts.post_author = 3
    			AND wp_posts.post_status = 'private'
    		)
    		ORDER BY wp_posts.post_date DESC
    	)
    )
    ORDER BY wp_posts.post_date DESC
    LIMIT 0, 20
  4. scribu
    Member
    Plugin Author

    Posted 1 year ago #

  5. duck__boy
    Member
    Posted 1 year ago #

    Have gone throught the chart, and with all other plugins deactivated and using the default theme, I'm still being told that no articles are found.

    Thanks.

  6. duck__boy
    Member
    Posted 1 year ago #

    Ok, so I have found out what is going on.

    I have a small hack in wp-includes/query.php, allowing me to show two types of posts in an Archive.

    if(is_archive() && !is_tax()){
    	$post_type = array(
    		POST_TYPE_NEWS,
    		POST_TYPE_TLA
    	);
    }

    I'm not quite sure how yet (still investigating), but this was causing the query to screw up some how. I've had this hack for about 3 years and this is the first time I've run in to issues, so I will be working on a work-around asap.

    With regards to the 'admin_column' query_var, could I request a future enhancement to allow me to change the title that is displayed in the list table? Although it's called Staff linked to this Story when I regester the connection type, I'd like for just Contacts to be used as the column title.

    Thanks.

  7. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    I have a small hack in wp-includes/query.php, allowing me to show two types of posts in an Archive.

    That's why kittens run when you hack Core. That should be perfectly achievable with the 'pre_get_posts' hook.

    With regards to the 'admin_column' query_var, could I request a future enhancement to allow me to change the title that is displayed in the list table?

    Yeah, it's on the TODO list: https://github.com/scribu/wp-posts-to-posts/issues/192

  8. duck__boy
    Member
    Posted 1 year ago #

    And I have the answer.

    It was because when Posts are listed in the admin they are an Archive, so when filtering the correct post_type ('person', for my Custom Post Type) was being over-ridden by my hack, causing the Query to fail. I've replaced the hack with the below, and it seems to be working just fine.

    add_filter('posts_where', 'add_tla_to_archive');
    function add_tla_to_archive($where){
    
    	if(!is_admin() && is_archive() && !is_tax()) :
    
    		$where = str_replace("$wpdb->posts.post_type = 'post'", "$wpdb->posts.post_type IN ('".POST_TYPE_NEWS."', '".POST_TYPE_TLA."')", $where);
    	endif;
    
    	return $where;
    
    }

    Thanks.

  9. scribu
    Member
    Plugin Author

    Posted 1 year ago #

    You can make it even more robust, like so:

    if ( !is_admin() ) {
    	add_filter('posts_where', 'add_tla_to_archive', 10, 2);
    }
    
    function add_tla_to_archive($where, $wp_query){
    	global $wpdb;
    
    	if ( $wp_query->is_archive() && !$wp_query->is_tax() ) {
    		$where = str_replace("$wpdb->posts.post_type = 'post'", "$wpdb->posts.post_type IN ('".POST_TYPE_NEWS."', '".POST_TYPE_TLA."')", $where);
    	}
    
    	return $where;
    }

    This ensures that you don't stomp unrelated WP_Query instances.

  10. duck__boy
    Member
    Posted 1 year ago #

    This ensures that you don't stomp unrelated WP_Query instances.

    Good point, have amended my code.

    Thanks for your help.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.