When you have a few thousand entries and use post_type
in combination with meta values, the MySQL query starts running very, very slowly because it seems to do a full table scan on the wp_postmeta
table. The most direct solution is to add an index on the wp_postmeta
table to avoid the full scan.
This is the SQL to add the index:
ALTER TABLE `wp_postmeta` ADD INDEX `key_value` (`meta_key`(20), `meta_value`(20)) USING BTREE;
The speed improvement was somewhere around 100x. I needed the first twenty characters of each field — you could alter it to be more or less.
Related Posts:
- add_filter > posts_where works partially
- Media library – Limit images to custom post type
- Using meta_query, how can i filter by a custom field and order by another one?
- Fail to compare dates in meta_query
- post_type_link filter causes 404 on the CPT page it’s used on
- order by meta_value serialized array
- Using Query Posts With Multiple Post Types And A Taxonomy
- Display custom post types with custom date field value (before today) & order by custom date field
- WooCommerce product search titles only
- Setting proper query for multiple custom admin filters
- How to query custom post types posts filtered by multiple custom taxonomies through a form selection
- Get posts between custom dates
- filter custom post type by meta key in dashboard
- How to filter wp_list_categories output with some custom post type meta query?
- A method for ordering mixed dates in search result loop (theory only, no actual code)
- search suggest – filter post type
- Querying multiple values from a single key
- find custom post type post by searching its custom field with my string
- Use WP_query to match post types based on custom field values
- Insert custom taxonomy into category query
- Group Custom Posts Types in a Relation field of ACF
- Enable shortcodes on custom post type
- How to Create movie schedule by custom post type query
- Filter RSS feed for custom post type to events for next 7 days only
- Custom Query With Multiple Meta Key Value
- Simple Share Buttons Add Plugin and Custom Post Type
- Trying to set up a range filter for related custom post types
- How can I filter records in a custom post type list in the admin based on the ACF field in the post that contains the current user?
- CPT Meta Searching
- Custom Post Type | Fatal Error on register_post_type()
- How to check if “is single” page
- Set object terms not working on plugin activation
- How to add the post ID (or any other post data) to a Contact Form 7 mail?
- Custom query shows custom post types in trash
- Multiple post type queries (with specific arguments for each)
- Filtering by Post Meta Custom Fields – Performance
- GravityForm: Populate Dropdown with custom post type [closed]
- Make a function to run only for CPTs
- Modifying date filter on admin page for custom post type to link to custom field
- Ignore sticky posts if post is not in meta query
- how to add extra meta box or textarea into custom post types
- Is it possible to apply a meta_query to one specific post type in a query with multilple post types?
- Query that joins the postmeta table twice
- Add html to cpt main page / admin edit.php
- Combine query_posts() and get_posts() into single query
- how can i get posts from custom post type particular taxonomy category
- register_taxonomy and register_post_type does not work [closed]
- Setting posts per page in query_posts
- How to insert content from another Custom Post type into Post?
- Pre_get_posts filter overwrites all search functionality
- “add_post_type_support” with Custom Post Type & ACF
- meta_query compare >= not working but
- Shortcode insertion in tab
- Filter categories of posts with checkboxes
- Pagination is not working on single-{slug}.php but works fine on page-{slug}.php
- Can’t get order_by meta_value_num to work properly
- How to quickly reorder posts in the admin panel that will persist for the wp-api
- Sort by custom field is not working
- advanced search forms with 3 input text and that the main problem 3 input text
- Add CPT as subpage under custom page
- Custom query works but returns “Undefined Offset: 0”
- check if post title in a custom post type exists in page
- Sorting multiple custom post types without a meta key/value pair by sort order
- Custom post types instead of regular post’s categories in a page template
- Change or update WordPress loop based on dropdown selection
- Pull a post based on a meta value in a custom post type
- How to detect filter in URL in Category page?
- How to hack YARPP plugin to find related posts for custom post type?
- Custom query to filter posts that have current post as a taxonomy [closed]
- Why is conditionally loading a custom plugin’s code only on a specific custom post type causing the site content to disappear?
- Show listings from Impress Listing plugin in random order using taxonomy and terms
- Update menu when saving settings
- WP_Query: include custom post type only with specific meta value
- Sorting Custom Post Type by Meta Value but wont display when set to 0
- One-to-many post relationships that are displayed by category (using posts-to-posts plugin)
- How to alter WP-JSON main query or best practise for custom endpoints
- What’s a better alternative to this code?
- Returning a list of custom post types excluding those without a specific meta_value
- Display custom post type from dynamic custom field
- custom post type not showing in menu
- Custom posts don’t work
- Query/list all terms and their custom post count
- Append date to custom post type url slug
- How to retain $_POST data when submitting form to custom page
- Why get_posts() not returning only selected category posts from Custom Post Type?
- Two near-identical custom field types – one works, the other doesn’t . What can cause this?
- single-{cpt}.php ignored
- If post has custom field then display css-class
- Archive for custom taxonomy lists all posts instead of current taxonomy
- Genesis filterable portfolio isotope [closed]
- Adding a custom post type taxonomy template in plugin
- Replace text in post from cvs
- Insert and then update post_type by wp_update_post
- Display only one post each WEEK
- wp_query check if integer exists in custom field’s array
- List children on child post
- How can I see a list of my Custom Post Types of the last term I was in?
- How to query_post custom posts within default taxonomy?
- Filtering custom post type on a combination of custom taxonomies and custom fields?
- Custom Post Types. Are there any disadvantages/advantages in using a plugin to develop them?