Custom post status not appearing - wordpress

I am building a directory theme for a client of mine, and I like to add the feature of expiration in the posts by modifying the post status from publish to expired.
To achieve that, I am trying to register a new post status by using the following code:
add_action('init', 'registerStatus', 0);
function registerStatus()
{
$args = array(
'label' => _x('Expired', 'Status General Name', 'z' ),
'label_count' => _n_noop('Expired (%s)', 'Expired (%s)', 'z'),
'public' => true,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'exclude_from_search' => true
);
register_post_status('expired', $args);
}
The problem is that I cannot see the registered post status either in WordPress posts, either in my custom post type post statuses.
Am I doing something wrong?

Thanks to Ryan Bayne I was able to add a custom post status to the admin panel on the edit post page. There is no wordpress filter avaiable. His solution with jQuery is perfect. Here the code if anyone else is searching for a solution:
add_action( 'post_submitbox_misc_actions', 'my_post_submitbox_misc_actions' );
function my_post_submitbox_misc_actions(){
global $post;
//only when editing a post
if( $post->post_type == 'post' ){
// custom post status: approved
$complete = '';
$label = '';
if( $post->post_status == 'approved' ){
$complete = 'selected=\"selected\"';
$label = '<span id=\"post-status-display\"> Approved</span>';
}
echo '<script>'.
'jQuery(document).ready(function($){'.
'$("select#post_status").append('.
'"<option value=\"approved\" '.$complete.'>'.
'Approved'.
'</option>"'.
');'.
'$(".misc-pub-section label").append("'.$label.'");'.
'});'.
'</script>';
}
}

Custom post status functionality is still under development (as it has been for the past four years!), see https://core.trac.wordpress.org/ticket/12706, and comments on https://wordpress.stackexchange.com/q/67655/25765. More useful info here: https://wordpress.stackexchange.com/search?q=register_post_status.
Personally, I'd strongly discourage implementing custom post statuses, but if really necessary, you could take a look at how the Edit Flow plugin handles it.

This feature is still pending for future development
NOTICE:
This function does NOT add the registered post status to the admin panel. This functionality is pending future development. Please refer to Trac Ticket #12706. Consider the action hook post_submitbox_misc_actions for adding this parameter.

Now November 2014 and still issues with custom statuses. I think the original code posted is fine. Here is a video showing an issue you will run into when implementing custom post status though. There may be a workaround i.e. hooking into the posts query and doing a custom query but I've not started the research.
Screencast of posts not showing in the All table when a custom status is applied, however the posts can be found in the table view for each custom status. Click here to view short clip.
That screencast was taking while I worked on my new WTG Tasks Manager plugin. I will leave my design in the plugin as it is and hopefully it helps to encourage improvements in this area of WordPress.
For proper answer...my custom status do show on the Edit Post screen for my custom post type so that is possible. If you want to take a look at my plugins registration of custom post type and statuses go to directory "posttypes/tasks.php" and play around with a working example. Here is the plugins official page...
https://wordpress.org/plugins/wtg-tasks-manager/

Related

Rank Math metabox preview for custom field

I've created a custom field in Rank Math...
add_action( 'rank_math/vars/register_extra_replacements', function(){
rank_math_register_var_replacement(
'op_shortcode',
[
'name' => esc_html__( 'OP ACF Field', 'rank-math' ),
'description' => esc_html__( 'Custom ACF field from ACF shortcodes.', 'rank-math' ),
'variable' => 'op_shortcode(field-name)',
'example' => esc_html__( 'Chrisad field value', 'rank-math' ),
],
'get_op_shortcode_callback'
);
});
function get_op_shortcode_callback( $shortcodename, $post_id ) {
global $post;
$post_id = "option"; // options page
$shortcodename = get_field( $shortcodename, $post_id, true );
return $shortcodename;
}
... it works in the frontend but I can't get it to be previewable in the backend metabox.
I wrote to Rank Math and the answer they gave first was, "There’s no additional code to be added to the filter since the custom variable is already working on the front end. For the preview to work, you need to ensure that the current content editor you are working on has WordPress editor support." (Which it does as far as I know because every other Rank Math variable is showing in the preview metabox).
Then they said, "When this happens it means that the custom variable you created contains code that cannot be rendered in the backend because the data is still not yet available." (I don't know what this means.)
And then finally, "The following article explains the default WordPress hooks firing sequence: http://rachievee.com/the-wordpress-hooks-firing-sequence/. Beyond that article, you will have to research further into how to get the variable to load. ACF support/forums might be able to help." (That's a 2015 article on hooks firing sequence which leads me to believe that there is a hook firing out of sequence but I can't figure it out).
I am just looking to see if anyone has any idea how to get the custom field I've registered to show up in the preview metabox in the backend (it is showing in the frontend).
Hope that makes sense.
Any help or insight is appreciated.

Wordpress page slug conflicts with media library item

I created a new page which is assigned a custom template. When I visit that page's url I see what appears to be the default page layout (not my template) and the admin toolbar shows options pertaining to media (ex: Edit media).
After some head scratching, I deduced that somehow that url must point to a media item. I edited the page slug and "bingo" the actual page appears just as expected. When I visit the original url (from the first slug) I see the same media item.
Bottom line: It appears that coincidentally the page and the media item share the same name, and this was somehow causing WP's wires to get crossed.
My question: Can someone help me understand how/why this happens? Does wordpress create magic permalinks to everything in the media library (other than their location in wp-content/uploads/...)?
Note: The media item was uploaded normally into the media library (not FTP into the root directory, etc)
Yes, in WordPress you cannot have duplicate slugs/categories/taxonomies/tags. So if your theme allows for media files and permalinks to have their own page and the slug is the same as another one, it will usually append a number to it because the database does not like it.
media slug "example"
page slug "example" will not work since that slug exists already , if done in the admin it will automatically change the slug to "example-1".
I just had this problem and fixed it like this:
$post_s=get_posts('posts_per_page=-1');
foreach($post_s as $p){
$atts = get_posts('post_type=attachment&name='.$p->post_name.'&posts_per_page=-1&post_status=inherit');
foreach($atts as $att){
echo 'found!! '.$p->post_name;
// Update post 37
$my_post = array(
'ID' => $atts->ID,
'post_name' => $att->post_name.'-image'
);
// Update the post into the database
wp_update_post( $my_post );
}
}
This is a late answer, but I wanted to give a cleaner version of the answer that alesub gave.
function wp21418_append_to_post_name() {
// Checks to see if the option images_updated has been set or has a value of true.
if ( get_option( 'images_updated' ) === 'true' ) :
return;
endif;
// get all attachment posts.
$attachments = get_posts([
'post_type' => 'attachment',
'post_status' => 'inherit',
'name' => $p->slug,
'posts_per_page' => -1,
]);
// For each attachment, loop and update the post_name
foreach($attachments as $p){
$attachment = array(
'ID' => $p->ID,
'post_name' => $p->post_name.'-image'
);
// Update the post into the database
wp_update_post( $attachment );
}
// Once everything is looped, add the option to the database.
add_option( 'images_updated', 'true' );
}
add_action( 'after_setup_theme', 'wp21418_append_to_post_name' );
This function runs on an action hook right after the theme has setup. The first line checks to see if there is an option in the database images_updated. If that option exists, we bail on the function and it doesn't do any processing. Otherwise, if the option does not exist, it runs the function and sets the option at the very end.
This makes it so it will only run once. You don't have to remove the function after refresh. If you want to run it again, you can simply remove the if statement at the top. As a caveat: doing this will add another -image at the end of post_names even if they have -image already (e.g. -image-image)
There could be more file name checking for that situation. Will update the answer with that if someone really needs it.
I tried one of the suggested solutions, and ended up having attachment pages like /bob-image-image-image-image/.
I'd suggest that instead of using alesub or disinfor's code blindly, use a better option in the form of "Disable Media Pages" plugin.
https://github.com/joppuyo/disable-media-pages
It automatically sets all attachment slugs to an unique id, and there is the option to mangle any existing attachment slugs so they won't cause any issues in the future.

XMLRPC & MetaWeblog, how can i create a page, create a post, and attach the page to this post

I want to create my own custom posting interface for my Wordpress blog, my reason for this is because I post multiple articles, sometimes ranging from 100-150 articles per day, and I want to simplify the process of posting an article, like batch posting articles.
I did my research and I found that Wordpress XMLRPC and MetaWeblog API is what I need.
So I tried it and I successfully post an article to my blog using this code:
<?php
include("../wp-includes/class-IXR.php");
$client = new IXR_Client('http://www.example.com/xmlrpc.php');
$content['title'] = 'Test Draft Entry using MetaWeblog API';
$content['description'] = '<p>Hello World!</p>';
if (!$client->query('metaWeblog.newPost','', 'admin',’password’, $content, false)) {
die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
}
echo $client->getResponse();
?>
But for each post that I create in my Wordpress blog requires several step:
Step 1: Create a new Background with a Background type of Youtube and enter the Youtube ID of this background.
Step 2: Add a new post and attach the previously published background to this post.
Step 3: Enter a custom field called artist_id in the custom field section and add an excerpt for this post, publish the post.
So, each article needs 3 steps. So my question is, how can I use the XMLRPC to perform these actions?
For adding excerpts use
$content['mt_excerpt'] = 'Your post excerpt';
For custom fields use
$content['custom_fields'] = array(
array( 'key' => 'artist_id', 'value' => '777' ),
array( 'key' => 'background', 'value' => 'background_value' )
);
Background and youtube metaboxes will most likely add custom post meta. You can find the key for them from db or source code and use it in above code.

Multiple Authors on same post in Wordpress

I have a friend who has asked me to build a site for him and two friends to write movie reviews. I'm pretty good with Wordpress, so it was the obvious choice for the site. The only difficulty I have is that they each plan to write a review on the same movie, and I can't think of how to achieve multiple authors in one post.
I've checked out a few plugins such as Co-Author Plus which allows multiple authors credited to the same post, but it doesn't provide the functionality for keeping each author's content separate.
The only solution I can think of is to use custom fields, but I would prefer if the authors can use the main content editor for their reviews. Any help is greatly appreciated!
Like I said, it should be better to have 1 review = 1 post.
So, I think the best way to achieve this should be to create post types :
movie
review, with a movie reference field
And modify post template to display movie and associated reviews on the same page.
An alternate solution should be to use taxonomy to handle movies and attach post to them.
I ran into the same problem with trying to figure out how to do this as well and I followed soju's advice and came up with a solution. There might be a better solution to this, but this is how I went about it. Me and my friend are doing an anime review blog and both me and him will be writing a review on the same anime.
I first created two post types:
anime: main page on the specific anime, like the description, pictures, etc.
reviews: the author's review on the anime. the options i have enabled on here are the editor, the title, and the author. Along with the associated anime taxonomy. That's all that's needed here
Then I created a taxonomy for anime titles so when a user needs to write a review on an anime that hasn't been added as a review yet they can add the title into the taxonomy.
I associated the taxonomy to both post_types and wala! That's pretty much all you need.
So now when you want to write a new review for a new anime you add a anime post type first and write down what the anime is about and include pictures etc. Add the title to the taxonomy and check it. After that you then create a new post of the post type reviews and write your review, remember to check the correct title in your taxonomy for what anime this is going to, then you are ready to go!
Problem 1: How do I include this into my loop?
Well you don't want to include both post types in your loop you just want to include posts and the other post type anime in your loop so you do the following in your functions.php file:
function include_custom_post_types( $query ) {
global $wp_query;
// Get all custom post types
$custom_post_type = get_query_var( 'post_type' );
// Get all post types
$post_types = get_post_types();
// If what you are getting is a category or a tag or that there are no custom
// post types you just want to set the post types to be the current post types
if ( (is_category() || is_tag()) && empty( $custom_post_type ))
$query->set( 'post_type' , $post_types );
// Set the custom post types you want to ignore
$ignore_types = array('reviews');
//Unset the post types that are gonna be ignored
foreach($post_types as $key=>$type)
{
if(in_array($type,$ignore_types))
{
unset($post_types[$key]);
}
}
// Set the post types for the query
if ( (is_home() && false == $query->query_vars['suppress_filters']) || is_feed())
$query->set( 'post_type', $post_types);
return $query;
}
add_filter( 'pre_get_posts' , 'include_custom_post_types' );
Problem 2: How do I display the reviews?
I solved this by creating another single.php file and renamed it to single-post_type_name.php. So in this case i created a single-anime.php file for my post type anime. Then in place of the contents i want to get all the reviews for this specific anime so I added the following within the file in the main content area:
<?php
//You grab the taxonomy that you have selected for this post
$terms = wp_get_post_terms(get_the_ID(), 'animes_reviewed');
// This is the args array for the criteria that the posts need to be in
$args = array(
// This is the post type of where your reviews are at
post_type' => 'reviews',
// this is for searching the taxonomy usually it's
// taxonomy_name => checked_taxonomy
'anime' => $terms[0]->name,
'post_status' => 'publish'
);
// Grab the posts
$posts = get_posts($args);
//Here I echo out the information for debugging purpose, but
//Here is where you can do HTML to display your reviews
foreach($posts as $post)
{
echo($post->post_content);
the_author_meta( 'nickname', $post->post_author);
}
?>
You can do a lot more with this adding more taxonomies etc. I have actually implemented an episode review by just adding a taxonomy and adding a criteria to look for in the post section. Hopefully this will help you out, might be a bit late though :( Thanks soju for recommending the custom post types!

How do I add content types other than Posts in the WordPress admin area?

I want to add a completely custom content type to the WordPress admin panel as per my image below. I don't believe this is called a plugin as I did a tutorial on those and they don't have an admin interface. I want to define a custom create/edit/delete screen for this content.
Is this possible?
What should I be searching for to get help on this?
I believe what you're looking for (as of Wordpress 3.0) is custom post types. There's quite a good tutorial on them here, however googling "wordpress custom post types" should provide a plethora of links.
http://codex.wordpress.org/Function_Reference/add_submenu_page is a good starting place to look. It will explain how to add new items into the menu.
Now, removing them is a different story. This function I wrote for one of my plugins removes entries from a submenu:
function cleanup_menu() {
global $submenu, $wpdb;
$new_submenu = array();
$remove = array( 'Cast Manager', 'Seating Manager', 'Download Reports', 'new report' );
foreach ($submenu['menuname'] as &$item) {
if ( ! in_array( $item[0], $remove ) ) { $new_submenu[] = $item; }
}
$submenu['menuname'] = $new_submenu;
}
So in your case, "menuname" would be changed to "Posts", I would guess. There's also a function remove_submenu_page, but there's no documentation on it and I haven't looked into it.

Resources