• Resolved Lawrence Eagles

    (@lawrenceagles)


    Hello, first I would like to say thanks for the awesome plugin but what I would like to know is that is there a way for me to include post status as one of the criteria. I noticed post status used by the plugin is set to published. I’m using the plugin for a unique purpose and would like to see the related post of my custom post status. Please a easy to follow well written codes as I have seen in the documentation so far would be appreciated like a gold mine by me.

    Thanks in advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author keesiemeijer

    (@keesiemeijer)

    Hi Lawrence David

    The only way to do this is by by replacing part of the query.
    Try it with this in your (child) theme’s functions.php file.

    
    add_filter( 'related_posts_by_taxonomy_posts_where', 'rpbt_add_custom_post_status', 10, 4 );
    
    function rpbt_add_custom_post_status( $where_sql, $post_id, $taxonomies, $args ) {
    
    	// The statusses you want to add.
    	$custom_status = array( 'future', 'draft' );
    	$search        = "post_status = 'publish'";
    
    	foreach ( $custom_status as $status ) {
    		$replace = $search . " OR post_status = '" . $status . "'";
    		$where_sql = str_replace( $search, $replace, $where_sql );
    	}
    
    	// If you want to remove the 'publish' status remove the two forward slashes from the next line.
    	// $where_sql = str_replace( $search . ' OR', '', $where_sql);
    
    	return $where_sql;
    }

    In this example the stati future and draft are added with this.

    
    $custom_status = array( 'future', 'draft' );
    

    Change it to your stati.

    Thread Starter Lawrence Eagles

    (@lawrenceagles)

    Thanks your code works well. The only thing is that I don’t want post with the post status of ‘publish’ to show.

    You code enabled my other custom post status to show up put still show the post that are published I want to show only post from my custom post status.

    Thanks for your prompt response you are awesome.

    Plugin Author keesiemeijer

    (@keesiemeijer)

    Try changing this

    
    // $where_sql = str_replace( $search . ' OR', '', $where_sql);
    

    To this

    
    $where_sql = str_replace( $search . ' OR', '', $where_sql);
    
    Thread Starter Lawrence Eagles

    (@lawrenceagles)

    You sir, are a genius!

    Plugin Author keesiemeijer

    (@keesiemeijer)

    You’re welcome ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Is there a way to use post status as one of the criteria’ is closed to new replies.