New answers tagged

0

save_post didn't work for me. What works in my situation is using content_save_pre filter or the content processing before save. Since I'm using Advanced Custom Field plugin for my cover_image field, and I ended up using acf/save_post filter to process the field.


0

If you are useing next_posts_link() && previous_posts_link() on your custom Query then you have to pass $max_pages parameter. like -> previous_posts_link( '« Previous', $loop->max_num_pages ); next_posts_link( '« Previous', $loop->max_num_pages ); Or if you want to use just previous_posts_link( '« Previous'); ...


0

Or you can use paginate_links: //First get a count of how many pages you'll be producing: $cat_id = 8; $per_page = 6; $all_posts = get_term_by( 'id', $cat_id, 'category' ); $posts_count = $all_posts->count ; $total_pages = intval( $posts_count / $per_page ) + 1; $current_page = max(1, get_query_var( 'paged' ) ); echo '<div class="pagination">' ;...


0

Try adjusting your $args. You will need to get the date from two days ago and then pass the year, month and day into the after argument as an array. Try this: $date = strtotime ( '-2 day' ); // Date from two days ago $args = array( 'post_type' => 'post', 'meta_key' => 'post_views_count', 'orderby' => 'meta_value_num', 'date_query'...


0

Only a guess but you might need global $post before your code. According to the Codex docs for the functions you are using the return values are Null if global $post is not set. Also you might want to use their suggested logicals: if (!empty( $prev_post ))


2

You can calculate offset via paged and posts_per_page. E.g: $per_page = 6; $paged = get_query_var('paged') ? : 1; $offset = (1 === $paged) ? 0 : (($paged - 1) * $per_page) + (($paged - 1) * 2); $args = array( 'order' => 'ASC', 'paged' => $paged, 'offset' => $offset, 'orderby' => 'ID', 'post_type' => 'post', 'post_status' =&...


1

image_send_to_editor hook will handle when the image is sent to the editor. The problem will be that your editor may not work correctly when you modify the URL -- use with caution. save_post will allow you to modify the $post content on save. update_(meta_type)_metadata could be used to intercept when the meta data is saved.


0

As per what I have understood is you have a Custom Post type as event and user role like Secretary. Now if a Secretary logged in , he can only be able to see his own events added, not the other one. So if the above is correct so for that, we will use pre_get_posts filter function wp_list_mine_posts( $wp_query_obj ) { // Front end, do nothing if( !...


1

Suppose you haven't modified anything to change the default content output, maybe this can help: add_filter('the_content', function($content) { $content = preg_replace('~^<p><img([^>]+)></p>~i', '', $content, 1); return $content; }, PHP_INT_MAX);


0

add_filter( 'bulk_actions-edit-weddings', 'remove_from_bulk_actions' ); function remove_from_bulk_actions( $actions ){ unset( $actions[ 'edit' ] ); return $actions; } Change "weddings" with the custom post name, if no custom post just use "post".


1

Here's a more readable version of the solution provided by Michael <?php $posts = new WP_Query(array('posts_per_page' => -1)); if ($posts->have_posts()): ?> <ul id="archives"> <?php $prev_month = ''; while ($posts->have_posts()): $posts->the_post(); if (get_the_date('F Y') != $prev_month): $...


1

In general the answer is yes and the web is full of good tutorials that would do a better job than I can here.


1

The below code check that post have category or not. If post have category then it will display list of category separate by comma. You can try this code. Hope this will help you. if(has_category()) { echo $cats_list = get_the_category_list(', '); }


1

Sadly the stock wordpress doesn't give much more help with custom fields. I'd recommend using the advanced custom fields plugin. https://wordpress.org/plugins/advanced-custom-fields/ You can set the name when creating a field and match it to what you've been using and it should work retroactively on old posts and content as well.


0

Did this before for a website. Here's what i did: Checked if there is a category with the same name as the title: if( term_exists(get_the_title(), 'category' )) If there was one then I got the category by slug: $cat = get_term_by("slug", $post->post_name, "category"); Checked if category had parent or was a parent: if($cat->parent !== 0) { ...


1

to use a translation not finished yet, click on "Export" here : https://translate.wordpress.org/projects/wp/dev/si/default . And save the files as si.po and si.mo in the directory wp-content/languages (choose the extension in the dropbox menu) If there is word missing, you can use Podedit to edit the file and you can also propose your word in the tool ...


0

Inside your parent post/category loop use this to generate a list of post titles/category names of the child posts/categories: $taxonomy_name = 'category'; $this_term = get_term_by( 'name', get_the_title(), $taxonomy_name ); $term_id = $this_term->term_id; $termchildren = get_term_children( $term_id, $taxonomy_name ); echo '<ul>'; foreach ( $...


0

Here you go. I think the first problem was calling do_shortcode($content) that gets called by add_shortcode() so it's redundant here. Then I think calling wpautop($content) introduced another problem rather than getting closer to a solution. function articlesection_rs_shortcode($atts, $content) { return '<span itemprop="articleSection">'. $content .'...


0

I am giving you an idea for this, you have to use jQuery for the popup and the below syntax will give you the current post data in a HTML popup. while ($the_query->have_posts()): $the_query->the_post(); echo '<div class="non-popup">'; echo '<div class="card-title" id="card-'.get_the_title.'">'.get_the_title().'</...


-2

You can use do_shortcode(get_the_content($id)); instead


0

Correct code: function tags_after_single_post_content($content) { $posttags = get_the_tags(); if ($posttags) { $array = []; foreach($posttags as $tag) { $array[] = '<a href="/tag/' . $tag->slug . '/">' . $tag->name . '</a>'; } $content .= 'Tags: ' . implode(', ', $array) . '<br>'; } return $content; } ...


0

Just realised what the problem is, the code should basically be: <?php $catergory = get_the_category(); ?> <?php echo $catergory[0]->name; ?> I think the different versions of PHP from localhost / server were different which is why it was working on one but not the other (feel free to correct me if I'm wrong)


0

You can use the function switch_to_blog() for this $other_id = 1234 // the id of the other blog to save the post to switch_to_blog($other_id); $my_post = array( 'post_title' => $post_title, 'post_content' => $post_content, 'post_status' => 'publish', 'post_author' => $post_author, ); // Insert the post into the database ...


0

I have flexible way where is possible to set any per_page value for each/any woprdpress site page: open functions.php of the current wp theme use smth like next: * add_filter('pre_option_posts_per_page', function($num) { if ($_SERVER['SCRIPT_URL'] == '/category/faq/') { return 999; } return $num; }); use for example http://php....


0

Correct. post__in requires an array but the id parameter in [video-course limit=8 id="418, 1186"] is passed in as a string, so in your shortcode, you would have to parse it into an array in order to use it: if ( !is_array($id) ) { // If $id is not an array (not your default value in your example) // then simultaneously parse it into an array and ...


-1

You just need add if condition here, check the following code query_posts('cat=2'); if(have_posts()) { /*continue with while code*/ } //end of if


0

You can and should do it in a more simple way: In this example you can see the $tags variable, it get the tags of the post compare them and shows 7 posts (the showposts parameter) also makes sure that it won't show the original post (post__not_in parameter). <?php // related posts based on first tag of current post $tags = wp_get_post_tags($post->ID);...


-1

there are many ways to this but i suggest ajax for it. When you click on any post get the post_id and pass it to ajax function. Get id in ajax function(in function.php ) you called first and get post data. You can do two things on ajax sucess 1) append complete html( html for popup ) OR 2) just append thumbnail, title,excerpt values in html


0

In general page URL are "last resort" match when nothing else fully matches the URL. This means that you can have something like example.com/category/cat1 be a URL of a category and example.com/category/cat1/page1 be a URL of a page. For the last to work you obviously need a grandpa page with slug "category" and a father page with slug "cat1". It is not ...


2

This little snippet could help you. $years = array(); if( have_posts() ) { while( have_posts() ) { the_post(); $year = get_the_date( 'Y' ); if ( ! isset( $years[ $year ] ) ) $years[ $year ] = array(); $years[ $year ][] = array( 'title' => get_the_title(), 'permalink' => get_the_permalink() ); } } This code gets ...


1

You can use get_term_by to get a category by name- $category = get_term_by( 'name', $post_slug, 'category' ); echo $category->term_id;


0

You have 2 ways. First is to write custom SQL query manualy for your request and loop that data. Second is to loop your data on standard way, save in multidimensional array, filter, transform and reorder that array via PHP how you need and after that loop that data and print like content. Booth ways are good but my recommendation is to write SQL query ...


0

First remove the category metabox from the post edit screen for this particular role, let's name it onecat-author: if (current_user_can('onecat-author')) { remove_meta_box ('category', 'post', 'normal'); } Next, when the post is saved use the available hook to assign the category: if (current_user_can('onecat-author')) { add_action ('save_post', ...


1

That is because you're trying to define a property that is already defined, query a table with a wrong name, loop through an integer while you can use get_results instead of query method of $wpdb ... I edited the code, hope it will help with your post duplication process: defined('WP_USE_THEMES') || define('WP_USE_THEMES', false); require_once('wp-load.php'...


0

It might be old but it showed up when I was looking for similar problem. Since WP 2.9.0 there is function called: wp_untrash_post(int $post_id) In my case it worked like a charm.


0

Answering my own question here, for anyone who might encounter this problem in the future. The cause for the right templates not loading was having uppercase letters in my template filename. Changing all characters in the filename to lowercase resolved the problem, and the templates are now being applied as they should. I'm still unsure about what is the ...


0

WordPress has a function wp_get_referer, which gets the referring url from the http-header. So, if you go from a category archive to a single post, the name of the category will normally be included in that url. This means you can start your single.php like this: $refer = wp_get_referer(); if (strpos($refer, 'category/cat1') != false) {... do something ...} ...


0

Google code for displaying custom post items. Fetch all the custom post items in an array and then use a simple html of a drop down and put it in a for loop which will go through all the elements of custom post type. In the value attribute of drop down use the current array value. Below is the html code for drop down wrap it in a for loop and add values as ...


-1

This will do something when you publish a post in the backend. function my_publish( $post_id ) { if ( ( $_POST['post_status'] === 'publish' ) && ( $_POST['original_post_status'] != 'publish' ) ) { error_log('WordPress would be better if it did not use Google Fonts. Looking forward to the system fonts.'); } } add_action( '...


-3

------------------ you can try this way, not 100% correct function.php - add admin ajax url ----------------- function enqueue_script(){ wp_register_script('custom_scripts',get_template_directory_uri() . '/js/scripts.js'); wp_localize_script( 'custom_scripts', 'ajax_url', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ))); wp_localize_script( '...


1

First, I would use WP_Query instead of the manual DB query (and don't tell me that's because of performance because you're querying 2 times the same thing). Second, you're saving your post as draft status and querying post with status publish


0

Try this code <div class="container"> <!-- Example row of columns --> <div class="row front-posts"> <?php if ( have_posts() ) : ?> <?php while ( have_posts() ) : the_post(); ?> <div class="line2"></div> <?php endwhile; ?> <?php endif; ?> <?php wp_reset_query(); ?> <!-- Start ...


1

There is no Page attributes because it's not a page and by default WordPress doesn't have an attribute to select template for post. The page templates are coded in the theme that you are using.It seems that the theme has a template called Full Width defined as page template so you are able to select it for pages. To not display the sidebar in the single ...


0

We can use simply https://wordpress.org/plugins/find-posts-using-attachment/ I hope it is the best way!


1

This is a partial answer, because as I mentioned in my comment I'm not sure why your code is requiring two 'really bad words' at the moment. It's also an untested attempt. I'll deal with your first question: how to send a user back to the posts page and display a suitable error message, rather than an error about editing posts in the trash. A way to do ...



Top 50 recent answers are included