Hot answers tagged

8

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

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

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


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

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

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

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

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

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

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

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

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


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

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

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.


2

In addition to Milo's answer: In wp::parse_request() the request URI is matched against the rewrite rules stored in $GLOBALS[ 'wp_rewrite' ]->rules. That means, the »route« will be parsed into query variables. The actual matching rule is stored in $GLOBALS[ 'wp_rewrite' ]->matches which is quite helpful for debugging: add_action( 'wp_footer', ...


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

I'm not sure why you need this or what is your exact use case here, but, in short, the_category() simply echos the returned value from get_the_category_list() which uses get_category_link() which uses get_term_link() to get the URL of the category page. This is where you should look if you need to change the URL of link linking to the category page. In ...


2

Maybe this is what you are looking for? Source is from @Michael Fields /** * Set default cat for cpt * @source {https://circlecube.com/says/2013/01/set-default-terms-for-your-custom-taxonomy-default/} * @source {http://wordpress.mfields.org/2010/set-default-terms-for-your-custom-taxonomies-in-wordpress-3-0/} * @license GPLv2 */ function ...


2

John there are lots of ways to do this, depending on what you want exactly and how much experience you have with html/php/css (or how much time you'd want to spend learning). There are plenty of plugins on wordpress that could achieve this. You might want to just try some of them out: https://wordpress.org/plugins/search.php?q=custom+header If you want ...


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

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



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