Hot answers tagged

11

Update 1: Thanks to @birgire for suggesting a better way: wp_list_categories( [ 'include' => wp_list_pluck( get_the_category(), 'term_id' ) ] ); Try this in your single.php template: $current_cats = get_the_category(); $current_cats_ids = []; foreach ($current_cats as $cat) { $current_cats_ids[] = $cat->term_id; } wp_list_categories([ ...


6

Here I tested this code and its working fine and steps here. I am just creating a dummy custom_post_type here book with the following code. function kv_custom_post_books() { $args = array( 'public' => true, 'label' => 'Books', 'taxonomies' => array('category', 'post_tag') , 'supports' => array( 'title', '...


6

Here's a script that you can enqueue into your admin panel. It will add a new tab to the category tabs called "Active". Whenever a checkbox is checked, it gets added to the "Active" tab list, you can also click links in the "Active" tab list to remove them ( uncheck them ). Add this as an external script, custom-tabs.js maybe: jQuery( document ).ready( ...


5

It's informative to check out the /wp-admin/post.php file, that contains the edit_post() function that calls wp_update_post(), which is a wp_insert_post() wrapper. Here's a skeleton for saving the assigned category terms: /** * Saving assigned category terms (skeleton) */ add_action( 'admin_action_editpost', function() { add_filter( '...


5

This is quite an interesting question (which I have upvoted, specially for your approach and research). The big curveball here is the first page of the query: You cannot set the query to return 0 posts on the first page By moving the page content of every page up by one page, you will loose the last page as the query will still only have the same amount of ...


5

Suppose articles is slug of the category. 1. Add a custom rewrite rule: add_action('init', function() { add_rewrite_rule('^articles/([^/]+)/?$', 'index.php?name=$matches[1]', 'top'); }, 10, 0); 2. Filter the post link: add_filter('post_link', function($post_link, $post, $leave_name = false, $sample = false) { if ( has_category('articles', $post) ...


5

There is extensive documentation on the database structure of WordPress in the Codex. Its pretty simple but still you probably do not want to mess with the data directly. As you mentioned CLI I'd recommend WP-CLI which offers some commands you need. A quick example from the docs to change the name of the term with the ID 15 from the taxonomy category to `...


5

You need to specify a post type in your WP_Query arguments when you need to query any other post type accept the build in post type post. By default, post_type is set to post, so when no specific post type is set manually by the user, WP_Query will query posts from the post type post Additionally, caller_get_posts have been deprecated for a very very long ...


4

You can do this using custom template and template_include filter. category-parent.php and category-sub.php both are not required. One can be category.php for parent categories and for child categories, you can use custom-category-child.php as category-sub.php is reserved for default hierarchy. First create a template file in theme custom-category-child....


4

From a quick look at the documentation for get_posts(), it doesn't look like you can exclude a post based on its category. EDIT: Actually, thanks to AntonChanning, turns out you can - just add &cat=-1 to your argument string, where 1 is the ID of the category you wish to exclude: $postslist = get_posts('numberposts=1&order=DESC&orderby=date&...


4

In addition to the answer by Tim, one can always use a proper tax_query. All the build in tag and category parameters gets converted to a proper tax_query before being passed to the WP_Tax_Query class to build the JOIN clause for the SQL query. I use a tax_query in almost all applications as it gives one a lot of flexibilty, specially when it comes to child ...


3

A better approach is to address the main search query. @Dipesh's answer is technically correct, but I would suggest using the pre_get_posts action instead to affect the intial query, rather than generating a new one. Much less work! add_action( 'pre_get_posts', 'se39294_search_pre_get_posts' ); function se39294_search_pre_get_posts( $query ) { if ( $...


3

If you want to list only child categories of the current category, set the child_of argument to the current category ID. wp_list_categories( array( 'child_of' => get_queried_object_id(), // this will be ID of current category in a category archive 'style' => 'none', 'title_li' => '' ) ); EDIT- To list only child ...


3

You probably have a read more tag in your posts, so you will need to remove them. We can use the the_content filter to achieve that. We need to search and replace the <!--more--> tag with nothing. You can try the following: (NOTE: The following code is untested) add_filter( 'the_content', function ( $content ) { return str_replace( '<!--more-...


3

Never ever use query_posts, it creates an endless amount of issues as it breaks the main query object on which so many build in functions, custom functions and plugin rely on. It is pure evil and you should avoid it as such as well. For custom queries, use WP_Query or get_posts, if you simply need to alter the current main query (except on single pages, ...


3

As I said, we would rather use native functions here, which is safer and already does all the hard work for you. We need to be very careful here as this is a very expensive operation to run. Not doing this correctly can crash your site due to timing out Your worksflow is also wrong here. I would first create all the tags, and after that, insert the tags ...


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

I would just set the post status for all posts for that particular category to private. If you already have a ton of posts, write yourself a small script using wp_update_post() to update all those posts' post status to private. Any posts published after this you can then just set to private (in the publish meta box) before publishing them. The main query ...


3

If you want your main query to take in account your custom post type, you can do as shown below : function add_custom_post_type_to_query( $query ) { if ( $query->is_archive() && $query->is_main_query() ) { $query->set( 'post_type', array('post', 'my_custom_post_type') ); } } add_action( 'pre_get_posts', '...


3

Here is some rough code for you, you may improve the query if you like... By the way selecting post_title and post_status is not neccessary, but useful if you want to ensure that you are returning the right results via conditional logic within your foreach loop. global $wpdb; $results = $wpdb->get_results( " SELECT ID, post_title, post_status ...


3

Lets use the build in features WordPress has to offer. It is almost always not adviced to use custom SQL whenever WordPress offers native functions to perform the specific duty. To query our posts, we will make use of WP_Query. The only problem is, WP_Query does not support the feature where we look for posts with certain post titles. Luckily, we can filter ...


3

You may need to include a tax_query for the Woocommerce taxonomy (called 'product_cat'): $tax_query = array( array( 'taxonomy' => 'product_cat' ), ); $query->set( 'tax_query', $tax_query ); } return $query; } However, you'll have to ensure that you can return posts AND pages AND product categories and also ...


3

There really is no sane way to accomplish this, specially if you have posts that are assigned to more than one tag (which is almost always the case). What is very sure, you have you work cut out for you. Here are some thought and ideas you can persue: POSTS WITH MULTIPLE TERMS If posts have more than one tag (or any term for that matter) assigned to them, ...


3

You can filter the posts list by appending ?category_name=xx to the admin posts list URL, and you can add a submenu page with that URL as the target via add_submenu_page: add_action( 'admin_menu', 'wpd_admin_menu_item' ); function wpd_admin_menu_item(){ add_submenu_page( 'edit.php', 'Page title', 'Menu item title', '...


3

With wp-cli installed you can run a bash script like this to remove the 'uncategorized' category from all posts with more than one category #!/bin/bash for post in $(wp post list --field=ID) do count=$(wp post term list $post 'category' --fields='name' --format="count") if [ "$count" -gt "1" ] then wp post term remove $post category '...


2

There's another way to do this using CSS and a pseudo selector to add the commas. You would just need to wrap each category in an HTML tag eg: foreach((get_the_category()) as $category) { echo '<span class="category">' . $category->cat_name . '<span>'; } And then with CSS: .category:not(:last-child)::after { content: ","; } EDIT: ...


2

I don't know if something's changed with the latest version of WP (4.4+), but is_main_query() will return a false positive as-is. It should be: function wpse63424_filter_pre_get_posts( $query ) { if ( ! $query->is_main_query() ) { return $query; } else { if ( is_category() || is_tag() ) { $query->set( '...


2

the id of the categories is autoincrement so you can sort by id to find the last one : $args = array( "type" => "post", "orderby" => "id", "order" => "DESC", "number" => "5", "taxonomy" => "category", "hide_empty" => FALSE, // TRUE or FALSE depending what you want ); $categories = get_categories($args);


2

As an alternative to editing the meta template, you can prevent the meta information from being displayed on single product pages at all. This can be handy if you also don't want to display the SKUs. Here is how to do it: remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 ); It goes to your functions.php.


2

How do the 'tag' and 'category' (default) taxonomies do 'save_post' action? They don't! Taxonomies aren't handled with a save_post action, it all happens directly inside wp_insert_post. You can see it in source here. You can use wp_set_object_terms in your callback to save terms from your custom metabox.



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