I suggest to use the Object Cache API or Transients API, whatever fits better your needs.
A very quick example with object cache:
// helper function
function get_my_object() {
$object = wp_cache_get( 'my_object' );
if ( false === $object ) {
$args = array( .... );
$object = new WP_Query( $args );
wp_cache_set( 'my_object', $object );
}
return $object;
}
class cyb_my_widget extends WP_Widget {
function __construct() {
// ...
}
function widget( $args, $instance ) {
// ...
$object = get_my_object();
}
function update( $new_instance, $old_instance ) {
// ...
}
function form( $instance ) {
// ...
}
}
Related Posts:
- When should you use WP_Query vs query_posts() vs get_posts()?
- Wp get all the sub pages of the parent using wp query
- How to query for most viewed posts and show top 5
- WP_Query vs get_posts
- Query Custom Meta Value with Increment
- Get posts by meta data OR title
- Perform query with meta_value date
- WP_Query ordered by custom field that is a date string?
- Which custom query am I in and how can I access its properties & methods?
- Retrieve or Query Pages by ID
- How do you query wordpress posts using a math formula between multiple meta field values?
- How to implement time filter to show random post 1 month for one category and 3 months for other categories
- Page navigation doesn’t show when query category
- How to query a custom post type with a taxonomy filter but display post type archive page?
- Change query_posts to WP_Query in page but does not work
- How to detect if query is runned from widget?
- Custom Query num_rows returns wrong amount
- Slow page loads due to WordPress Core Query
- Query Page Content From Theme Options?
- Is there a better way to pull in custom content without querying posts?
- How can I override one post and make it display content for another post?
- Include current post into loop
- Filter post query to only show direct children of category
- Should I reset $wp_query?
- Unable to paginate a custom page query
- Why doesn’t my WP Meta Query return any results?
- Filter products on category AND tag
- pre_user_query vs pre_get_posts
- Get posts that match defined arrays of tags
- I need query_posts() to order results first by a meta value and then by post ID
- How to show post title in content editor in backend?
- My entry results are not consistently alphabetized
- Merge two queries and remove duplicate
- Ordering Posts by parent category, name ascending
- Optimising specific Query with ACF meta objects
- How to organize a WP_Query’s list of posts by category and display category title?
- When should you use WP_Query vs query_posts() vs get_posts()?
- How to query post ids liked by the Author
- WordPress Query showing multiple titles
- Query Posts by date range with fixed beginning and end
- custom query to get posts
- Using OR conditions in meta_query for query_posts argument
- What is the most efficient way of querying posts based on visits and date for current day?
- Get posts by menu ID
- Meta Query with date and time on the same Day before given time
- Order posts by tags count?
- Add and in the header while looping over custom query in page template
- Single page theme
- How to speed up wp_query, took more 5s to run against 100k posts
- How can i make WP_Query return post image and categories?
- How add a custom posttype name using ACF field to a query post array
- Custom Post order for homepage
- Get image of latest post from taxonomies/categories
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- 2 loops on page – one with orderby rand second orderby date
- Undefined WP_Query::has_posts()?
- Is it possible to dynamically get queried term AND taxonomy?
- Create ONE callback for all page templates, post filter queries + paginated pages, triggering pagination via AJAX
- Pull post meta with post_query?
- How to access a query twice in different template files?
- Five posts from a category in footer
- Add quicklink to in the Admin posts page where I can query by a meta_key
- How to display multiple custom fields with the same meta_key in an ascending order?
- How to use the query hook/filter?
- Order by empty custom field
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- How to exclude post formats from wordpress recent posts in a tabs widget [closed]
- How to increase load time of an archive/search page (WP_Query)
- Easy way to write complex queries in wordpress
- Error with function in functions.php?
- get_posts output always same post
- WordPress Custom Search Form Displaying Unexpected Results
- Utilising an existing page while using the “s” query parameter
- Yoast primary category query modification
- Write WP Query that selects posts that are part of the same two categories
- Get next/prev image/attachment in time with date query
- Include one page/post into query which is already returning posts
- How to add a recent post function to a text widget I created for practice?
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- odd behaviours from template tags
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- Optimising WP_Query with ACF Fields
- How do I stop the same post showing multiple times in a archive?
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- How to get meta key list efficiently?
- Why pagination is not working with tax_query param?
- WP_Query & Duplicate entries
- Query by meta value (add a dropdown of all values)
- ACF: using two loops, the_field returns field content from another loop
- query_posts() doesn’t seem to be called in my page
- Fetching $_POST from Page Template into functions.php
- Query posts in current category but not attachment format
- wp_query – Modify $query to include duplicate content
- How to get several fields from wp_query?
- Pagination not working with WP_Query (creates links but no page)
- query_posts with sorting on a custom datestamp
- Query custom post type by date field only working when two posts match the query
- Divide WP_Query posts by date & post type
- query by meta value then date and not empty meta value
- New WordPress WP Query using posts from certain categories