Attachment Post Type is not working
-
Hello,
I am trying to display popular attachments at the home page (as I already did with posts) and for some reason wpp_get_mostpopular() is not working.
If I use the variable ‘post_type’ => ‘attachment’, it says: Sorry, no data so far.
This is the complete code:
$args = array( 'header' => 'Entradas más populares', 'range' => 'last30days', 'thumbnail_width' => 150, 'thumbnail_height' => 150, 'stats_category' => 1, 'post_type' => 'attachment', 'post_html' => '<li>{thumb} <a class="text-title" href="{url}">{text_title}</a> <p class="views">{views} Visitas</p> </li>' ); if (function_exists('wpp_get_mostpopular')) wpp_get_mostpopular( $args );It does not work in wp-admin/options-general.php?page=wordpress-popular-posts&tab=stats either.
But if I add wpp_get_views() to the attachment page it shows the correct views count.
So the numbers are stored in the database, but can´t be displayed.I also tried with [wpp post_type=”attachment”]
Do you have any ideas which could be the problem?
Thanks in advance,
Kind Regards-
This topic was modified 7 years, 7 months ago by
kowabungawp.
The page I need help with: [log in to see the link]
-
This topic was modified 7 years, 7 months ago by
-
Debug Info:
PHP version: 5.6.38
PHP extensions: Core, date, ereg, libxml, openssl, pcre, zlib, filter, hash, pcntl, readline, Reflection, SPL, session, standard, cgi-fcgi, bcmath, bz2, calendar, ctype, curl, dom, enchant, mbstring, fileinfo, ftp, gd, gettext, iconv, imap, intl, json, exif, mcrypt, mysqlnd, mysql, mysqli, PDO, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, pspell, SimpleXML, soap, sockets, sqlite3, tokenizer, xml, wddx, xmlreader, xmlrpc, xmlwriter, xsl, zip, mhash, ionCube Loader, Zend Guard Loader, Zend OPcache
Database version: 10.2.18-MariaDB
InnoDB availability: YES
WordPress version: 4.9.8
Multisite: No
External object cache: No
WPP_CACHE_VIEWS: No
Active plugins: Akismet Anti-Spam 4.0.8, Better Search Replace 1.3.2, Duplicate Post 3.2.2, Duplicator 1.2.52, Enable Media Replace 3.2.7, Frontend Uploader 1.3.1, Google Analytics Dashboard for WP (GADWP) 5.3.5, Media Library Filter 1.0.2, Smush 2.8.1, WordPress Popular Posts 4.2.0, wpDiscuz 5.2.2, WPForms Lite 1.4.9, Yoast SEO 8.4
Theme: GeneratePress Child (0.1) by Tom Usborne
Hola @kowabungawp,
(…) If I use the variable ‘post_type’ => ‘attachment’, it says: Sorry, no data so far.
It does not work in wp-admin/options-general.php?page=wordpress-popular-posts&tab=stats either.
Do you have any ideas which could be the problem?
Yes, that’s a known (but not documented) limitation of the plugin.
WPP lists popular posts/pages/whatever-post-types from the database with status publish. Attachments (and child posts) status is always inherit, which is the reason why the plugin returns “Sorry, no data so far”: it’ll never find attachments with status “publish”.
If you’re using the latest version of the plugin, then this is what you need to have WPP list attachments:
/** * Have WPP list attachments. * * @param string $where Original WHERE clause from WPP_Query class. * @param array $options Plugin settings. * return string */ wp4649_wp_popular_attachments($where, $options){ // Set post_status to "inherit" $where = str_replace("p.post_status = 'publish'", "p.post_status = 'inherit'", $where); return $where; } add_filter( 'wpp_query_where', 'wp4649_wp_popular_attachments', 10, 2 );Basically, we’re hooking into WPP’s WPP_Query class to filter popular posts by status ‘inherit’ instead of ‘publish’, which in conjunction with your ‘post_type’ parameter should return your most popular attachments.
Thanks so much Héctor! You are the man.
I just replaced this line to work with both post types (posts and attachments):
$where = str_replace("p.post_status = 'publish'", "p.post_status IN ('publish', 'inherit')", $where);
I am showing popular posts and popular images (separated).Now I have another problem.
The attachment query is not showing any thumbnails.
I tried the different options in tools section:- Featured Image (default).
- First image on post.
- First attachment.
But none of them worked.
Do you know a workaround to make the thumbnail take the picture from the corresponding attachment?
Maybe using wp_get_attachment_image();Thanks so much Héctor! You are the man.
Don’t mention it. Glad I could help.
The attachment query is not showing any thumbnails (…) Do you know a workaround to make the thumbnail take the picture from the corresponding attachment?
No idea, to be honest. Unfortunately, I’m flooded with work at the moment so I can’t look into this right away. I’ll get back to you on this one as soon as I can.
Hey @kowabungawp,
I had a chance to have a look at this just now. As you said, using the wp_get_attachment_image() function is the solution.
Follow these steps, but use this function instead:
/** * Parses custom content tags in WordPress Popular Posts. * * @param string $html The HTML markup from the plugin. * @param integer $attachment_id The attachment ID. * @return string */ function wpp_parse_tags_in_popular_posts( $html, $attachment_id ){ // Replace custom content tag {custom_thumb} with the actual custom thumbnail if ( false !== strpos($html, '{custom_thumb}') ) { // Get thumbnail $custom_thumb = '<a href="' . get_permalink( $attachment_id ) . '">' . wp_get_attachment_image( $attachment_id, 'my-custom-size' ) . '</a>'; // Replace content tag with thumbnail $html = str_replace( '{custom_thumb}', $custom_thumb, $html ); } return $html; } add_filter( "wpp_parse_custom_content_tags", "wpp_parse_tags_in_popular_posts", 10, 2 );You can see it in action here.
It worked, thanks so much!!
Don’t mention it. Glad I could help!
Hi Hector:
The code you noted above to include items marked as published or inherited:
/** * Have WPP list attachments. * * @param string $where Original WHERE clause from WPP_Query class. * @param array $options Plugin settings. * return string */ wp4649_wp_popular_attachments($where, $options){ // Set post_status to "inherit" $where = str_replace("p.post_status = 'publish'", "p.post_status = 'inherit'", $where); return $where; } add_filter( 'wpp_query_where', 'wp4649_wp_popular_attachments', 10, 2 );In which file(s) should that code be added?
Thanks.
-
This reply was modified 7 years, 2 months ago by
marckw.
Hey @marckw,
You can place that code snippet in your theme’s
functions.phpfile.Thanks Hector. I tried that initially and received a php error at the $where = str_replace… line.
May have been an error on my part; I’ll try again.
The topic ‘Attachment Post Type is not working’ is closed to new replies.