Tag Info

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 ) && ...


11

In the tutorial (Example 1), he has to declare the global $post so that he can access the post_parent from it. In a function like that, the $post is not a global variable unless he makes it so. In the codex (Example 2), it is declared global because the sample code is just a sample, explicitly trying to tell you that the $post variable is a global one. ...


9

I understand that post ID's are unique, but are they reliable as persistent data ID's? Yes.


9

There is a sad truth: you can never ever be sure that some code will not break your code, and there is nothing you can do to prevent that. Expecially in WordPress where everything is global. That said, yes, global $post is one of the most used global var, so using special care for it can be a good idea. In my code I rarely direct access to global $post. ...


7

The post format taxonomy: The post format is a default taxonomy, registered with: register_taxonomy( 'post_format', 'post', array( 'public' => true, 'hierarchical' => false, 'labels' => array( 'name' => _x( 'Format', 'post format' ), 'singular_name' => _x( 'Format', ...


7

The $post global variable is set by $wp_query->the_post() (WP_Post) inside the loop and is accessible during the loop. $post holds the post data from the current post. When you are inside the loop, you don't need to call the $post global, as it is already accessible. When you are outside the loop, and you need to access post data, you need to make use ...


7

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 // ...


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

You can add a filter to 'loop_start', count how may posts you have and inject the needed number of "fake" posts that are intances of WP_Post not having a reference in DB. add_filter( 'loop_start', function( $query ) { $module = $query->post_count % 6; $to_fill = $module === 0 ? 0 : 6 - $module ; if ( (int) $query->post_count === 0 || ...


5

If I understand well, user having a special role in your site should: Be able to edit own posts in all statuses but 'publish' and not be able to publish them, just send for revision Be able to edit others posts only when pending, but not be able to publish them, just send for revision Never be able to delete other posts, no matter the status If so, it ...


5

The pedantic answer is NO. While IDs are unique they can change without any change in UX as long as the change retains the consistency of the DB. And while creating a new post will generate a new unique ID, you can also create a post via code to reuse some "old" ID. In practice they are reliable, but if reliability is very important to you then you need to ...


5

Here's a sketch of another idea, where we create scheduling shortcuts to make it easier for the users: We can use the post_submitbox_misc_actions to add extra fields to the submit box: /** * Scheduling-Shortcuts fields. * * @see http://wordpress.stackexchange.com/a/168748/26350 */ add_action( 'post_submitbox_misc_actions', function() { if( ! ...


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 ...


4

next_posts_link and previous_posts_link (plural) are for archive pagination. For single posts you want next_post_link and previous_post_link (singular).


4

Use a custom view in the front-end: You can try to modify the SELECT queries in the front-end with the following (alpha) plugin: <?php /** * Plugin Name: wpdb - a custom SELECT view for the wp_posts table on the front-end * Version: alpha */ ! is_admin() && add_filter( 'query', function( $query ) { global $wpdb; $view = ...


4

You don't declare the order of the results when you register the post type. Instead, you do at the time the results are requested. If you're using WP_Query to get the results then you add your orderby and order arguments to the request. http://codex.wordpress.org/Class_Reference/WP_Query If you want to change the order of the results on the archive pages ...


4

If I understand well you want to show the last post (one post) from one of the 3 post types you have, using a dynamic url like http://example.com/latest. First of all lets add a filter to 'do_parse_request' filter: add_filter( 'do_parse_request', function( $bool, WP $wp ) { $home_path = trim( parse_url( home_url(), PHP_URL_PATH ), '/' ); $path = ...


4

Revisions to me are useless and just fills up your db. It might be useful to others though. Revisions are created each time a post is revised/updated. This just holds an archive/copy of the post before it was updated, and gives the user an option to restore the specific post to an earlier time. This works exactly like any system with a restore button. If ...


3

I suspect what you're trying to do is implement related posts, and you're using post meta to indicate which posts are related to the current post. So if I have a handbag, and there are 5 related products, the handbag product has post meta containing the IDs of those 5 products. While this sort of works, it doesn't scale, and it isn't performant. Instead ...


3

Try removing (image), like this: <?php $content = get_the_content(); $content = preg_replace("/<img[^>]+\>/i", " ", $content); $content = apply_filters('the_content', $content); $content = str_replace(']]>', ']]>', $content); echo $content; ?>


3

You are deleting all pages with that code because you are triggering the function wp_delete_post(). You should build the url to delete the page(or post) like this: $delLink = wp_nonce_url( get_bloginfo('wpurl') . "/wp-admin/post.php?action=delete&post=" . $page->ID, 'delete-post_' . $page->ID); So your code should be: $pages = get_pages(); ...


3

You could just rewind the query to loop through again: <?php rewind_posts(); ?> <?php while ( have_posts() ) : the_post(); ?> <p>Post Number: <?php the_ID(); ?></p> <?php endwhile; ?> THE LOOP


3

You could run three get_posts() calls to snag the latest post from each post type, and build the links from there. Something like this would output the most recent post from each of the three post types in an unordered list: /** * Grab the latest post from three different post types. * * @return array Array of post objects. */ function ...


3

WordPress uses the attachment filename to create the attachment post slug. If your file was named something else, there would not be any conflicts. If you have your post permalinks set to /%postname%/ , and you upload an image FIRST, and then create a post SECOND, then WordPress has to make a choice between the two when someone tries to access the ...


3

You might be running up against WordPress's global $blog_id variable in test case #2. Try this: foreach($sites as $my_blog_id => $name) { switch_to_blog($my_blog_id); $sites[$my_blog_id] = get_bloginfo('name'); restore_current_blog(); }


3

You quite likely need to simply visit your permalink settings page to refresh. Goto Admin > Settings > Permalinks. Then try your URL on the front-end again.


3

You can exclude posts from archive pages with thee help of pre_get_posts action. Usually pre_get_posts is used to modify main query, so it's best solution for your problem. function my_custom_get_posts( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_archive() ) { $query->set( ...


3

As stated in my comment to the OP, you should make use of pre_get_posts to target change the query variables as needed before the main query is executed. Just a tip, pre_get_posts uses the same exact parameters as WP_Query, so you can have a look at those parameters and use them to construct your pre_get_post action parameters to modify the query variables ...


3

If your entire page fits in the 4GB LONGTEXT post content field, the limit to the number of pages would be related to the amount of RAM available on your server. When a multipage post is loaded, it is exploded on <!--nextpage--> to put your post into an array of individual pages, so at that point your single page load is consuming at least twice the ...


3

As said in a comment, you can do this in one query. The principle here is to only display the date heading if the post date's month of the previous post does not match that of the previous post FEW NOTES Before I start, a few notes: Never use query_posts, except if you really need to break everything on a page. It not just reruns the main query and also ...



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