Hot answers tagged

10

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

The documentation for register_post_type mentions a taxonomies parameter, giving it a value of array( 'category' ) will do what you want. There is also the register_taxonomy_for_object_type function


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


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

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


4

The error says it all, you are sending the request to a invalid host. Change this: var ajaxurl = 'http://my-site.co.uk <?php bloginfo("wpurl");?>/wp-admin/admin-ajax.php'; To: var ajaxurl = "<?php echo esc_js( admin_url( 'admin-ajax.php' ) ) ?>"; Note: from your code and description I'm assuming you are generating the jQuery code within ...


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


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

You can do this with pre_get_posts. This hook is called after the query variable object is created, but before the actual query is run. For excluding category id 32 and 39 from homepage, you can setup a function like this. function wpse_exclude_categories( $query ) { if ( is_admin() ) return; if ( $query->is_home() && ...


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


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


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

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

To make this more complete, here is what I have said in comments in reply to the question on hand Just to quickly explain, WP_Query fails catastrophically in some cases where empty arrays are passed to some of its parameters, instead of also returning an empty array as we should expect, WP_Query returns all posts. As for getting the correct stickies, as ...


2

There is a build in function for this already, no need to create a custom function for this. The function is called get_term_children() and will return either An array of child terms if the given term have children An empty array if no child terms are found WP_Error object if the taxonomy does not exist With this in mind, wrap get_term_children() in a ...


2

The relevant code is here: query_posts("cat=25&orderby=DESCd&showposts=3&$randomArticlesNum&caller_get_posts=1"); That is the query that is pulling the posts, and that is what you want to alter. Your previous theme builder also did something that should never be done-- used query_posts(). Also, both showposts and caller_get_posts have ...


2

So, i used this script in the end, thanks to Pieter Goosen's post Category Alphabet List Broken $terms = get_terms('anime-on-going'); if ( !empty( $terms ) && !is_wp_error( $terms ) ){ $term_list = []; foreach ( $terms as $term ){ $first_letter = strtoupper($term->name[0]); $term_list[$first_letter][] = $term; } unset($term); ...


2

In order for this to work, you will need to know from where the single post was refered from and if the referrer is a valid one. Secondly, we need to adjust pagination between single posts in order for us to pass our referrer between single posts. The problem with referrers is, they are set and controlled by user side. Referrers can be disabled or blocked ...


2

You can follow similar approach with get_the_terms. Here is the function. function add_taxonomy_to_single( $classes ) { if ( is_single() ) { global $post; $my_terms = get_the_terms( $post->ID, 'custom-taxonomy' ); if ( $my_terms && ! is_wp_error( $my_terms ) ) { foreach ($my_terms as $term) { ...


2

This will definitly remove word category . Add this code in functions.php.As you already know woocommerce_template_single_meta is the hook responsible for meta information of the single-product. File is present in /woocommerce/templates/single-product/meta.php. Either you can use the following code to edit the html of meta.php or you can copy the file ...


2

parse_query is where the majority of the work is done. Query vars and is_ conditionals are set, and template-loader.php just checks those is_ conditional tags to load the appropriate template.



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