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
- Modify WordPress Search
- 2 wordpress loops showing 1 post from same post type – how to avoid showing the same post?
- How to get all post except particular category without breaking the paging
- How do I rewrite this loop as a new WP_Query style-loop?
- Transient api Caches confused
- Exclude All Posts Which Do Not Have A Tag Assigned
- Filter Custom Taxonomy Posts
- Is it possible to query from external database? [duplicate]
- Get all posts which was posted on X Days WordPress
- Can’t sort custom post type by random
- pre_get_posts – Trying to get property of non-object warning
- Query posts that contain video?
- Get multiple users with meta value in one query and populate WP_User class
- WordPress query posts with multiple post_meta data
- How to provide meta_key array to wp_query?
- Easy way to write complex queries in wordpress
- Error with function in functions.php?
- Order post type by sum of two custom fields
- Get a list of posts by specific category
- get_posts output always same post
- Show a 404 error page if Public query variable’s value doesn’t exist?
- WordPress Custom Search Form Displaying Unexpected Results
- Query_posts works when appending via AJAX call wp_query doesn’t?
- Use WP_Query or query_posts() or get_posts() for optimizing a site?
- Is there a way to make this kind of loop shorter and nicer?
- Query for first 3 posts to change the look and feel
- How to check the array values, what WP_Query has brought to me?
- How to create custom query by keyword in post title?
- How to make posts unqueryable/unpublish posts where ACF relationship field is an unpublished post? [closed]
- Conditional operator OR not working with custom fields
- complex get_posts() query to select child pages
- How to set up hierarchical relationships without using plugins / meta query
- How to add custom meta to ‘pre_get_terms’?
- Set front_page programatically after user login via query, while leaving site option alone
- Get posts that do not have the same tags as current
- Utilising an existing page while using the “s” query parameter
- Shortcode for custom query not returning results when attributes are added
- Yoast primary category query modification
- Query where ANDing slug values not working
- Posts Query | Query to only show sub categories
- How to give classname to post if post has no content?
- How to filter query loop block with a search string from the query parameters
- Update query for wp_posts and wp_postmeta
- Get posts in taxonomy randomly
- Posts query according to meta box date
- Get specific ACF key and value from all posts – no access to DB
- mysql query order by
- How can I comment comma-separated array values?
- How do I add a relation parameter to my filter query?
- How to create a widget like this?
- Notice thrown when creating numeric pagination on custom query
- wordpress udpate query not equal to
- How to generate feed for custom template
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- Write WP Query that selects posts that are part of the same two categories
- Creating attachments archive in tags and categories
- How to use ‘WP_Query’ or ‘query_posts’ to display content in a descending order
- Get next/prev image/attachment in time with date query
- WP_User_Query orderby meta_val_num