You can add a timestamp to your post meta each time a product is viewed, then query the five most recently viewed products.
Assuming you are using a custom post type named ‘product’, add the following inside the loop of your single-product.php template file:
<?php
if (get_post_type( $post->ID ) == 'product' )
update_post_meta( $post->ID, '_last_viewed', current_time('mysql') );
?>
To display the five most recently viewed products:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'meta_key' => '_last_viewed',
'orderby' => 'meta_value',
'order' => 'DESC'
);
query_posts( $args ); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h2><a href="https://wordpress.stackexchange.com/questions/76564/<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Related Posts:
- framework for plugin/theme options panel? [closed]
- How to include jQuery and JavaScript files correctly?
- Single functions.php or split into many small files?
- How can I configure Docker for developing and deploying a custom theme?
- What is an alternative method to the WordPress private _doing_it_wrong() function
- What is the difference between esc_html and wp_filter_nohtml_kses?
- jQuery in header or footer
- Update exisiting site to 3.5 release candidate
- add_filter and remove_filter added before and after wp_query
- What is the difference between strip_tags and wp_filter_nohtml_kses?
- Having Problem On Getting WP Post Gallery Images URL
- What’s the difference between hooks, filters and actions? [duplicate]
- How to: get main plugin/theme file?
- add_theme_support using a plugin
- How to bundle a plugin with a theme, or vice versa
- How to add Font Awesome 5 icons in WP Admin dashboard menu?
- How Can I setup WP CLI on Windows development machine running AMPPS?
- WP_LOCALIZE_SCRIPT doesn’t work
- What function to hook for changes made in status and visibility of a post
- Example of uninstaller routine to remove all custom theme options from wp_options
- Modify a Free Plugin available on wordpress.org & include with my Premium Theme? [closed]
- Execution limit and Memory limit errors even i changed to 1024M and 600(cache.php,load.php)?
- Are we allowed to use the Allman (BSD) indent style when coding WordPress plugins and themes?
- Best choice of options/settings framework for plugin/theme development
- Is there any way to have Featured Text, as opposed to Featured Image?
- Find source of notice / warning / errors efficiently
- StackExchange clone using WordPress?
- Sharing common functionality (functions, template parts) between plugins and themes?
- Display content from custom post without modifying the single template
- WordPress not working on localhost
- Can I change header.php of current theme through a plugin function?
- Creating a Link Text like Submit Button in Admin Page
- SQL query for custom taxonomy slugs
- Tried in different ways but sidebar not working?
- Short code not working in boostrap modal dont no why?
- Is there a way to list all actions registered by a plugin or theme?
- How to hook a custom user function to a wordpress core ajax action?
- Elementor custom Query with ACF fields to show matching woocommerce products custom fields
- WP Gutenberg Blocks – How to limit first/root blocks
- How developed with version control word press site on shared host? [closed]
- Following WordPress Markup Is mandatory, got awkward resultyby wordpress standard markup?
- How do I create plugin or theme using MVC pattern?
- When is the proper time to minify css and js with git workflow?
- How to Get Current Custom Post Type Associated Taxonomy Term
- Plugin templates vs Parent Theme
- Warning: include(): https:// wrapper is disabled in the server configuration by allow_url_include=0
- Plugin is not generating title tags on any pages or posts
- Is it necessary to do validation again when retrieving data from database?
- Can’t change the style of a submit input type? [closed]
- Make a plugin page out of influence of the theme’s style
- Update Data parameter of a wp_localize_script() call
- Custom entity search and display
- get_the_tags with separator control?
- 400 Bad Request, in wordpress theme development, wp_ajax
- Add child pages to submenu automatically
- Why in this archive page that call query_posts() function show only the last 10 posts?
- Issue on Setting $icon_url Parameter on WP add_menu_page()
- Why would you use esc_attr() on internal functions?
- Ajax: Populate with content from a post’s ID not working – duplicating current page html instead
- How to find where an object first instantiatiation
- Full documentation about $args for register_rest_route?
- How to replace settings in WordPress plugin from a theme
- Save temporary registration data
- WordPress dynamic widget by location?
- Setting a post’s category
- How can the_excerpt (or equivalent) be called on a category description?
- Change the behaviour of a button
- WP_Query order posts by category
- Configuring Xdebug with docker compose
- How to access index file in Block Themes?
- How to show comments from different Plugins to same post type?
- WordPress Favicon not Working For Images/Videos/PDFs
- Want to know how to reveal a WordPress theme, considering the theme name is hidden?
- Redirection from a specific page for users logged in but not with membership
- Metabox types list
- Is it possible to develop themes and plugins locally while still allowing content to be updated directly in the admin dashboard?
- Replace “content-area” of themes 404 page with plugin?
- Autoloading & Namespaces in WordPress Plugins & Themes: Can it Work?
- Custom premium registration form and profile page for a WordPress Web Application
- How do I make two shortcode use the same id and increment it when I use them multiple times?
- Post Pagination does not working on WP-Query
- How to destroy or dispose wordpress uploder/manager?
- How to CRUD from wordpress database in wordpress theme?
- Caption Shortcode: what filter to change the image size?
- Font Awesome stopped showing icons, shows &# text instead [closed]
- To remove rendering of menus and header, plugin or theme?
- Managing Custom Designed Content
- Issue on Getting WP Gallery Items In cpt-single.php As Attachments
- Generate activation url
- Display Data’s metabox in page
- Ajax functions – no access to wp-admin.php only online
- WordPress Beginer to Guru [duplicate]
- Saving multiple fields as array
- How can I make my website with wordpress having on-spot editing feature as compared to concrete5 CMS?
- Invalid Menu Items
- Gutenberg text field validation
- How to create A – Z List with pictures?
- How to customize password reset message page on success if no error in password reset
- useBlockProps() nests wrapper with class name inside block wrapper in the editor
- User set default settings for a Block in Site Editor