Hot answers tagged

14

I don't think there exist one, but you could create your own, wpse_empty_trash, with something like this: /** * Add a custom hook 'wpse_empty_trash' */ add_action( 'load-edit.php', function() { add_action( 'before_delete_post', function ( $post_id ) { if ( 'trash' === get_post_status( $post_id ) && ...


8

There is no special hook to author change. But you can achieve it by using post_updated hook. Example: add_action('post_updated', 'prefix_on_update_author', 10, 3); function prefix_on_update_author($post_ID, $post_after, $post_before) { if ($post_after->post_author != $post_before->post_author) { // author has been changed // ...


8

UPDATE 21-01-2016 19:35 SA TIME - BUG FOUND!!!!! YEAH!!!!!! I finally found the bug. As you stated in your last update, the failure only happens when $post_content has a <!--nextpage--> tag in the content. I tested it, and did confirm that any other page after the page after the <!--nextpage--> returns a 404 and then the page gets redirects back ...


7

The following code will work, but you just have to keep in mind that conflicts can happen easily if the slug for your custom post type is the same as a page or post's slug... First, we will remove the slug from the permalink: function na_remove_slug( $post_link, $post, $leavename ) { if ( 'events' != $post->post_type || 'publish' != ...


6

Use get_edit_post_link filter. add_filter('get_edit_post_link', 'get_edit_post_link_178416', 99, 3); function get_edit_post_link_178416($link, $post_id, $context) { $scr = get_current_screen(); if ($scr->id == 'edit-post' && $context == 'display') { return 'http://google.com'; } else { return $link; } } You can ...


6

If you view the Media Library in the list mode: /wp-admin/upload.php?mode=list then you will see the Attach/Detach links for each attachment. Each attachment can only be attached to a single parent through the post_parent field in the wp_posst table. Deleting an image from the post editor will not change the post_parent field to 0. Making life a little ...


5

I tried to figure this out not long ago and the short answer from what I know is no. Not from within the rewrite argument at least. The long explanation becomes apparent if you look at the actual code of register_post_type in wp-includes/post.php line 1454: add_permastruct( $post_type, "{$args->rewrite['slug']}/%$post_type%", $permastruct_args ); You ...


5

Yes. Use $post->guid instead of $post->ID. NB: guid must be in lowercaps for it to work. Detailed explanation here: http://wordpress.stackexchange.com/a/209832/71131 Some additional detail on the purpose of keeping $post->ID as a backup in your third-party service: http://wordpress.stackexchange.com/a/209833/71131


5

Note that there's a syntax error for all those three examples you provided: add_filter('content_pagination', 'custom_content'), 10, 2); add_action('the_post', 'custom_content')); add_action('template_redirect', 'custom_content')); where an extra ) is added. Replace these lines to: add_filter( 'content_pagination', 'custom_content', 10, 2); ...


5

This is doable, and as I stated in a comment, you need to follow the following steps Get a count the amount of sticky posts. This can be done by simply counting get_option( 'sticky_posts' ) which holds the ID's of the sticky posts in the form of an array You would also need to get the amount of posts per page. You don't want to hardcode here. The amount of ...


5

Some of the code in OP is a bit old and depreciated, like caller_get_posts which was depreciated years ago. The correct parameter to use now is ignore_sticky_posts. Your query is also really inefficient and not good for performance. Here is how I would tackle this issue Use get_queried_object_id() to get the current post ID instead of the more unreliable ...


5

Actually this was quite easy – when you know what's missing: The current post ID for the global $wp_embed object, so it knows what to refer to. The reason is simple: oEmbeds get cached as post meta data, so without knowing the ID, the MarkUp can't get fetched and replaced in the content. // grab a post from the database /** @var \WP_Embed $wp_embed */ ...


5

Post ID is always incremental. There's no way the ID of a previous post is assigned to a new post. If a post is deleted, the ID of that post remains vacant forever.


4

You can try following code: get_post_field( 'menu_order', $post_id);


4

I was curious about this and checked the Codex on keyboard shortcuts, but didn't find it mentioned there. I searched and found out that this seems to be already solved, e.g. here and here. I haven't tested out these other plugins so I'm not sure how they solve it, but I decided to take on the challenge and see how this could be solved ;-) So here's my ...


4

New content pagination filter in WordPress 4.4 As of WordPress 4.4 we can use the content_pagination filter ( see ticket #9911 ) /** * Filter the "pages" derived from splitting the post content. * * "Pages" are determined by splitting the post content based on the presence * of `<!-- nextpage -->` tags. * * @since 4.4.0 * * @param array ...


4

this the default template priority structure for categories: category-slug.php category-ID.php category.php archive.php index.php so if your category slug is "car", you can add file named category-car.php make sure you resave permalinks after adding the file, if it doest work :) more details here


3

There are several ways to deal with this problem. You could have a look for some syntax highlighter plugin, those will also bring you coloured text with highlighted keywords etc. pp. This is probably nice, if you use code quite often. I use SyntaxHighlighter Evolved. If you want to do this by hand, <code> is probably the HTML-element you want to use, ...


3

First, you should always use the WordPress AJAX methods, not a custom function for that. See AJAX in Plugins in the Codex. With that practice in mind, you can set up your request like this. Change the AJAX URL to <?php echo admin_url('admin-ajax.php'); ?> and add the actionyou want WordPress to execute in the next step to your data . ...


3

You could use the filter term_links-post_tag to sort tags while ignoring their case: add_filter( "term_links-post_tag", 'themeslug_sort_tags_alphabetically' ); function themeslug_sort_tags_alphabetically( $tags ){ natcasesort( $tags ); return $tags; } This code is to be placed in the functions.php of the child theme.


3

You could use the Metadata API to retrieve the rw_advertising_position metadata for each post, seperate the ads from the content, and then re-insert the ads at the proper locations: /** * Extracts from an array posts with positional metadata and re-inserts them at the proper * indices. See http://wordpress.stackexchange.com/questions/210493 **/ function ...


3

All those complex functions can be reduced to one simple function: attachment_url_to_postid() You only need to parse the image URL to retrieve the attachment ID: $attachment_id = attachment_url_to_postid( $image_url ); echo $attachment_id; That's all you need.


3

Your function will never "get" $post as this is not how functions work. Functions cannot pull anything into itself by itself. If you have debug turned on, you will get a definite bug notice that $post in undefined..... You would need to invoke the $post global inside your function for anything to work that relies on the $post global, so the following would ...


3

EDIT - Now that 4.4 is out, you should use the content_pagination filter. See birgire's answer below. You can add formatting to raw post content by applying the content filters directly to $post->post_content: echo apply_filters( 'the_content', $post->post_content ); This will bypass pagination by not using the get_the_content function, which is ...


3

I believe you are talking about single post pages. The template hierarchy does not make provision for single templates according to the category a post belongs to, so single-{$category}.php does not exist. To make single-{$category}.php work, we can make use of the single_template filter add_filter( 'single_template', function ( $template ) { global ...


3

You can pass variables to javascript using wp_localize_script function: https://codex.wordpress.org/Function_Reference/wp_localize_script Add the following to functions.php if(!function_exists('load_my_script')){ function load_my_script() { global $post; $deps = array('jquery'); $version= '1.0'; $in_footer = true; ...


3

You may do this: $args = array( 'posts_per_page' => '-1', 'post_type' => 'portfolio' 'tax_query' => array( array( 'taxonomy' => 'portfolio_cat', 'field' => 'name', 'terms' => array('cat1', 'cat2', 'cat3')) ) ) ); $wp_query = new WP_Query( $args );


3

I'm not going to discuss implementation of a slider, that is too broad. There are a couple of tutorials around which you can look at on how to implement simple sliders The real issue here is the query itself. Sorting a query by the terms a post belongs to is quite heavy operations, and if not done correctly (no offense, as you have done in your answer), it ...


3

You can use the has_password parameter of WP_Query. Here's an example how you can hide it, for non administrators, on the edit.php screen for the post post type: /** * Hide password protected posts, for non-admins, in the case of 'edit-post' screen id * * @link http://wordpress.stackexchange.com/a/200426/26350 */ add_action( 'pre_get_posts', function( ...


3

In response to my previous answer: you could of course set the rewrite parameter to false when registering a new post type and handle the rewrite rules yourself like so <?php function wpsx203951_custom_init() { $post_type = 'event'; $args = (object) array( 'public' => true, 'label' => 'Events', 'rewrite' ...



Only top voted, non community-wiki answers of a minimum length are eligible