Tag Info

Hot answers tagged

10

If you're on a remote server, XMLRPC would be best, but requires login details If you're in a PHP script on the same server, wp-load.php would be best ( XMLRPC will involve a request of sorts ) If you're in a bash or CLI script, WP CLI would be best, e.g.: wp post create --post_type=page --post_status=publish --post_title='A future post' ...


8

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


7

Post list in admin (edit.php) use a normal WP_Query, just like frontend can be changed using pre_get_posts. add_action('pre_get_posts', 'reorder_my_cpt'); function reorder_my_cpt( $q ) { $s = get_current_screen(); // change 'book' with your real CPT name if ( is_admin() && $s->base === 'edit' && $s->post_type === 'book' ...


7

You have 2 problems. First problem The line $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; will fail, because on singular post view, when the url contains '/page/XX/' the variable WordPress sets is 'page' and not 'paged'. You may think to use 'page' instead of 'paged', but that will not work too, because once the 'page' variable ...


7

This question is going to get some opinion based answers, but well, I'll give it a try. There are a couple of reasons why someone might've told you that, it would be great to know the context also. Upto version 3.4 or 3.5, that function only worked when the parameter was a var, that might be one of the reasons why someone told you that. I, myself, don't ...


7

For starters let's dive into what is 5 really. It is the post's ID. But what is ID in turn? It is value in the MySQL table row which identifies the specific post record. Issues with using IDs So first there are some conceptual problems with it. It's not content. It's not something user creates, manipulates, or (most of the time) aware of. WP is (by design) ...


6

Prevent authors from publishing too short content: Here's one idea using a custom post status, for example short: /** * Register a custom 'short' post status * * @see http://wordpress.stackexchange.com/a/159044/26350 */ function wpse_short_post_status() { register_post_status( 'short', array( 'label' => _x( 'Short', ...


6

The Automatic Sponsor Posts Injector: Here's one idea based on my answer for the question: How to show Y number of custom posts after every X normal posts? I hopefully made it a little bit more useful here on Github, but it may be refined much more (future work). The SponsorPostsInjector class will help you to automatically inject the sponsor posts into ...


6

If I understand well, you need to retrieve all the posts having category 'y' and also meta key 'facebook_shares', after that, you need the position of a specific post. My proposal is: Create a class that extends WP_Query and filter the results for required category and meta field. This class should also filter the SQL query to set the meta value as a ...


5

When you set a post as password protected, the protection happen on get_the_content() function. There WordPress check for a post password cookie, and if not set, not valid or expired then show the password form. This password form is submitted to wp-login.php, there a cookie is setted according to the password wrote in the form, then the request is ...


5

If this is how it looks: <img class="alignleft size-medium wp-image-8530" alt="..." src="#" width="413" height="275" /> <img class="alignleft size-medium wp-image-8529" alt="..." src="#" width="413" height="275" /> Then you have to put them together like this: <img class="alignleft size-medium wp-image-8530" alt="..." src="#" width="413" ...


5

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

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


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


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

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


4

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


4

You can use 'the_title' filter (Codex link) add_filter( 'the_title', 'lose_four_chars'); function lose_four_chars($title) { if ( is_single()) { return substr($title, 4); } else { return $title; } }


4

The following query retrieves the oldest post of a specified user/author: $user_id = 42; // or whatever it is $args = array( 'posts_per_page' => 1, 'post_status' => 'publish', 'author' => $user_id, 'orderby' => 'date', 'order' => 'ASC', ); $first_post = new WP_Query($args); if ($first_post->have_posts()) { ...


4

When inside of the media uploaded in the post edit screen, just search for your previously attached media and embed it in your new post. The relationship between posts and attachments is preserved in the database. While an attachment can only belong to one post, it can still be embedded in many posts without worry.


4

You can technically add a post column with SQL but I'd caution against it as backup scripts, exports etc. would likely ignore it. Instead, I would add the content as a post_meta, either using a Custom Field or through PHP with the update_post_meta() function. To fetch a post based on the meta simply use: $args = array( 'post_type' => ...


4

The answer by bungeshea is not ideal as in many cases: You don't want to strip everything from <br>, <p> etc. You want it as a default behavior for your WP visual composer which the above code will delete In many cases it is considered as "hacking the core" as this is changing the default core behavior of WP - for example such a thing will not ...


4

Archive Page An archive page is part of the WordPress Template Hierarchy, and is the template file WordPress uses to display the archive index list for a given post type. The custom post type archive template hierarchy is as follows: archive-{posttype}.php archive.php index.php WordPress uses the query parameters to output this page, and posts are ...


4

Preface Once post preview is frontend, the comment form depends on how theme handle it. I will assume in this answer that the comment form is shown using the standard comment_form() function. The previous assumption isn't enough, in fact comment_form is probably the function in WordPress core with more hooks. There is almost an hook for every line. So what ...


4

You can use a modified version of this answer by @s_ha_dum. Where instead of an option you can setup a user meta (even if meta query is slower than option query) add_action('save_post', 'user_last_update', 10, 2); function user_last_update($id, $p) { if ( (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') ...


3

The problem is that changing the post visibility to private invokes a status change, in a way private is not only a visibility but also a publishing status. Or to quote the Wordpress Codex, section »Content Visibility - Private Content«. Once you change the visibility to private, the post or page status changes to "Privately Published" as shown. ...


3

My idea is that you can setup a post date for the attachments. This can be done using attachment_fields_to_edit to show the UI (is possible use the touch_time internal function). After that, you can filter all the attachments query to show only the attachments with a past or current date. So add_action('load-post.php', 'setup_attachment_fields'); ...


3

You cannot replace the main query with the output of $wpdb. The main query is a WP_Query object. $wpdb returns a simple array or object, but not a WP_Query object. Even if you could shove raw SQL into a query (something in the back of my head makes me think you can but I can't swear to it) it is not advisable. Try var_dump($wp_query); and you will see that ...


3

It generally the clears the current loop // main loop <?php if (have_posts()) : while (have_posts()) : the_post(); ?> <?php the_content(); ?> <?php endwhile; endif; ?> // rewind <?php rewind_posts(); ?> // new loop <?php while (have_posts()) : the_post(); ?> <?php the_content(); ?> <?php endwhile; ?> Here it ...


3

You should not be using query_posts at all. It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats ...



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