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


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

Every post update in wordpress, is handled by the wp_update_post function. This function has some defaults, and for post_content_filtered the default value is '' (empty string). Once the default are merged to args passed to function via wp_parse_args it means that every time a post is updated and post_content_filtered is not explicitly passed, it is setted ...


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


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


6

I tried for a bit and could not get this to work perfectly, but it's close, it's hard to extend with the <form> for the popup styles being hardcoded, thought possible with more work. To get started you can: Enqueue the link popup javascript and styles, the main .js file is wp-includes/wplink.js. Depending on where you loading this you might need to ...


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

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


6

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


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


4

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


4

I found the following filter, here unlocking all protected meta data: add_filter( 'is_protected_meta', '__return_false' ); Or it can be fine tuned: add_filter( 'is_protected_meta', function( $protected, $meta_key, $meta_type ) { $allowed = array( '_edit_lock', '_test', '_wp_page_template' ); if( in_array( $meta_key, $allowed ) ) return ...


4

The problem with the code in your question is it always intercepts. You completely lose the ability to not publish, no drafts possible. Why this leads to untrashable post is something I didn't inspect further. Anyway this isn't what one wants. I'm glad the javascript solution is working for you, but personally I have two problems with it, first, you can ...


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

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


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

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


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

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; } }


3

There are several hooks in get_the_post_thumbnail for example begin_fetch_post_thumbnail_html end_fetch_post_thumbnail_html /wp-includes/post-thumbnail-template.php You could add a function to one of these actions which removes your filter on first run. If you need further help with this please add some code how you filter the_content.


3

Alternatively to Kaiser's answer, when you register the post type, simply use the taxonomies attribute: register_post_type( 'my-cpt', array( ... 'taxonomies' => array( 'category' ), ... ) ); Details on register_post_type() can be found here in Codex.


3

Which one uses less resources? Neither, really. I don't need any support for plugins, theming, etc. I think you've answered your own question. Go for the latter technique (wp-load.php).


3

'the_posts' is an action fired everytime WP_Query get posts, for main query and for other secondary queries, so when you do something like: $foo = new WP_Query($args); in a widget or elsewhere (shortcode, related posts...) 'the_posts' is triggered again. However there is only one main query, so you can use a conditional and do what you do only if the ...


3

It won't be very hard to do. Just add this to your functions.php or into your plugin: function my_edit_per_page( $result, $option, $user ) { if ( (int)$result < 1 ) return 20; // or whatever you want } add_filter( 'get_user_option_edit_page_per_page', 'my_edit_per_page', 10, 3 ); // for pages add_filter( 'get_user_option_edit_post_per_page', ...


3

Firts / last post make sense if we have an order to refer to. get_next_post() function use post date to decide which post is the next. It optionally use also taxonomy, but once you use the function without any argument, only date is relevant, so the first post is the most recent post in same post type. To get the most recent post you can use a new WP_Query, ...


3

I know this has been answered and the answer solved the issue but I found it very poor for any newbie that want to learn WordPress so I give mine in the hope it will be better: While get_posts() can do the job I would have used a simple query, for a secondary loop I prefer creating my own separate instance of the WP_Query Engelen is right there's no use ...


3

By default, no, WordPress doesn't keep track of who changes post statuses (at least that I can see). you can hook into transition_post_status and log the user id. add_action( 'transition_post_status', 'wwm_transition_post_status', 10, 3 ); function wwm_transition_post_status( $new_status, $old_status, $post ) { if ( 'trash' == $new_status ) { ...


3

get_post_class() generates and returns the list of classes echoed by post_class(). The only reason for the function itself not producing the result you want can be found in the source post-template.php: if ( ! is_admin() ) $classes[] = $post->post_type; Which means that the class post actually only gets generated if is_admin() ...


3

You don't have to run two queries, as you already got what you want (even more). $args = array ( 'post_type' => 'mytheme_posttype', 'post_status' => 'publish', 'numberposts' => 12, ); $sliderQuery = new WP_Query($args); $num_posts = $sliderQuery->post_count; if ($num_posts >=4) { if ($num_posts >= 12) ...



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