Hot answers tagged

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

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

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


4

You can use the category__in argument: // use get_the_category( $post_id ) if you are outside the loop $categories = get_the_category(); if( $categories ){ $related_posts = get_posts( array( 'category__in' => $categories ) ); }


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


3

// Getting Parent ID $parent = get_term_by( 'name', 'Session 2015', 'category'); // Checking if SubCategory Exist. $term = term_exists('Photos', 'category', $parent->term_id); //if Exist Do the query if ($term !== 0 && $term !== null) { $query = new WP_Query( 'cat='.$term['term_id'] ); } References: term_exists get_term_by


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

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

I think you have misinterpreted custom post-types and custom taxonomies, somewhat. Theory Custom Post Types I consider the purpose of a custom post-type to be format of data distinct in it's own right - it describes a type of content, but not the content itself. Take the in-built page, post, and attachment post-types, for example: The page post-type ...


3

This same exact question was asked earlier this week or over the weekend, and it had me thinking. Here is the idea that I came up with. If you look at the source code of the WP_Query class, you will see that sticky posts is only added to the first page of the home page. There is also no filter supplied to change this behavior in order to set the required ...


3

Use the terms_clauses filter, which passes all the various components of the query (fields, join, where, orderby, order & limits), and implement your own "search" argument: function wpse_178511_get_terms_fields( $clauses, $taxonomies, $args ) { if ( ! empty( $args['surname'] ) ) { global $wpdb; $surname_like = $wpdb->esc_like( ...


3

A lot of time since this quesiton and answer were posted. Since then things has changed a lot. The typical recommendation about disallow crawlers to access wp-content/themes, wp-content/plugins, wp-content/cache, wp-includes, and any other directory that contains CSS or js files needed in the site, are no longer valid. For example, lets talk about Google. ...


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

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


2

In general you can't. ID numbers given at one site might be already used at another.


2

The theme uses the_excerpt() instead of the_content(). I had created a child theme before (so I don't modify the source theme directly: only the child one), so I only had to copy/paste the PHP file (archive.php) from source theme to child theme, and change the_excerpt() for the_content( __( 'Read more &#8250;', 'my-domain' ) );


2

As I have stated, never ever use query_posts. The page you have linked to in the codex is worth nothing and completely wrong. For extra info on why not to use query_posts, check my answer here and the answer by @Rarst. You have another issue here, category_name does not accept the category name as value, but the slug. The naming convention of this parameter ...


2

You could set up a custom post type with custom taxonomies and then allow people to filter the results or do a search. Failing that, perhaps an event plugin would help. It depends on how much coding you want to do or if you want to try and find a plug and play solution.


2

cracks knuckles Alright -- so first thing is first. Working w/ WordPress Databases Read this: https://codex.wordpress.org/Class_Reference/wpdb Avoid write SQL statements outside of the $wpdb object. Avoid writing SQL statements like wp_users instead do this: $users = $wpdb->get_results( "SELECT FROM $wpdb->users" ); This will help if you ever ...


2

SQL-wise, you only need to join to the wp_posts table once. Joining to the terms stuff will give you multiple rows, so it's probably easiest to group these and then use GROUP_CONCAT() to flatten the terms into a comma-separated string (updated to use LEFT joins): global $wpdb; $sql = $wpdb->prepare( 'SELECT p.ID, p.post_title AS Product, ...


2

CHRISTMAS EDITION Due to Christmas time and my brain hitting some serious technical difficulties, I got get_the_category() and get_the_categories filter mixed up. Here is the revised filter function, working: You can also make use of the get_the_categories filter add_filter( 'get_the_categories', function ( $categories ) { // Only return the first ...


2

Try this code, not much different from your's but with proper nesting. Let me know if that works. <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> <a href="<?php the_permalink() ?>" class="noborder"><?php if ( ...


2

I cannot really see doing this than to run a couple of queries here, one per category. We will need to be clever here to avoid a lot of unnecessary work. Lets look at the code; (which I will comment to ease the process of understanding) // Get the categories. We will only get the category ID's to speed things up $category_args = [ 'fields' => ...


2

Try adding the Thumb Column instead of re-defining the whole columns. function custom_columns($columns) { return array_merge( $columns, array( 'thumb' => __('Thumb', 'mytextdomain' ), ) ); } Also, make sure if you accidentally unset the Categories column when you removed the Tags, Author and Comments columns. EDIT After ...



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