WordPress.org Meta Team Fixes Search Snippet Issue with Download Page Promoting WordPress.com – WP Tavern

[ad_1] Yesterday evening Chris Klosowski, Sandhills Development Partner and Director of Technology, tweeted out a problem with the way WordPress.org’s Download page was appearing in Google’s Search results snippets when searching for “WordPress.” Underneath the link, the preview text referenced WordPress.com’s hosting: WordPress.com is the easiest way to create a free website or blog. It’s a powerful hosting platform that grows with you. We offer expert support for your WordPress site. Others reported seeing the intended description when Googling, which is designated in the Schema.org tag in the head tag but not printed on the page: “Download WordPress today, and get started on creating your website with one of the most powerful, popular, and customizable platforms in the world.” The reference to WordPress.com came from the hosting providers listed at the top of the page, where it randomly displays two upon each page refresh. The Download button used to be at the top of the Download page but ever since mid-January 2021, it has been pushed further down below recommended hosts. This is presumably to help people who want to set up a self-hosted site but don’t know where to get started. “Google was skipping our defined page descriptions in favor of some in-page content,” WordPress lead developer Dion Hulse said, regarding the issue with the search results snippet. The WordPress Meta team was alerted to the problem and quickly put a solution in place to encourage Google to look somewhere else on the page for the main content. “The Download page has info about the mobile apps and hosting for WordPress,” core contributor Corey McKrill wrote in the commit message. “These are in section container elements, which might be the reason that Google is using the content of the hosting container for its search result snippet, instead of the meta description tag. By changing these containers to aside elements, hopefully Google will get the message that they don’t contain the most pertinent information for that page.” The meta team also marked the hosting recommendations on the download page as exempt from being included in the Google search result snippet, so that it doesn’t pull text from these aside elements. Here is what the updated search result snippet looks like after the changes were put in place: Klosowski’s tweet highlighted the perennial tension that arises from the confusion between WordPress.com and WordPress.org. The recommended hosting page has always been a contentious bit of real estate on WordPress.org but especially now that hosting companies are also prominently promoted on the Download page. https://twitter.com/cklosowski/status/1413264854643736577 In this situation, Josepha Haden Chomphosy, WordPress’ Executive Director, quickly acknowledged that the search snippet promoting WordPress.com was in fact a problem, heading off those who might promote the notion that it was intentional. The Meta team acted swiftly to resolve the issue and return the snippet to its former meta description. It is not known how long Google has been pulling from the text in the recommended hosts sections to populate the snippet, but the code is now more explicit about the fact that those companies are not the most important content on the Download page. Like this: Like Loading… [ad_2] Source link

Continue reading

Add Custom Link to All Posts Screen Based on Post Meta Data

[ad_1] TL;DR: This article outlines the code needed to add a custom link on the All Posts screen that uses a custom piece of post metadata. Note: A few months ago, I wrote an article on how to add a custom view to the All Posts screen. This article is not all together the same, but not all together different. Think of it as a more detailed and perhaps for more practical implementation of the concept. Assume that you have a standard post type or a custom post type and you’re going to simply filter by a headline that you define using a mechanism that allows you to save data to the post_metadata table. For example, let’s say that you have a post and it as a piece of meta data with: a meta_key with the value of article_attribute a meta_value with the value of headline And you want to use this information to add a new Headlines link that automatically filters everything out except articles with that metadata. Here’s how to do it. Custom Link on All Posts Screen Before getting started, it’s worth noting that there are two ways to go about tackling this problem: We could add the link at the top of the All Posts page first and then add the functionality for filtering the data second, Or we can do it the other way around where we add the backend logic first then add the All Posts page link. I’m going to opt to start with the second option. There’s no reason why it has to be done this way. It’s my preference. First, I need to hook into the pre_get_posts hook provided by WordPress. I’m not going to be using any namespaced classes or prefixed functions in this example (given that I’ve covered that content enough on this site), but I’ll have demo plugin for this linked at the bottom of the post. Anyway, I’ll start off by adding an anonymous function attached to the aforementioned hook: add_action( ‘pre_get_posts’, function ( WP_Query $query ) { // … } ); Notice that the anonymous function accepts a single argument which is a reference to the current instance of WP_Query that’s running on the page. If you’re not familiar with that class, then I’d recommend reading any of these articles or the Developer Resources page. In the function, I need to check for the presence of a meta_value in the query string. This is easy to do thanks to the filter_input function provided by PHP. add_action( ‘pre_get_posts’, function ( WP_Query $query ) { $meta_value=”headline”; if ( filter_input( INPUT_GET, ‘meta_value’ ) === $meta_value ) { $query->set( ‘meta_key’, ‘article_attribute’ ); $query->set( ‘meta_value’, $meta_value ); } } ); This hook will look to see if the headline value is key for the meta_value key in the query string. If so, it then adds a meta_key and meta_value to the instance of WP_Query which will instruct WordPress to retrieve all of the posts with just that metadata. After that I need to add a a link to the top of the All Posts page to trigger this functionality. To do this, I’ll leverage the views_edit-posts hook. This function will accept an array of anchors that will be displayed at the top of the page. I refer to these as $views so that’s what the function will accept when I stub it out: add_action( ‘views_edit-post’, function ( array $views ) { // … return $views; } ); Note that it’s important to return the array back to WordPress so that it knows what to render even if no modification is made. First, I need to determine if I’m currently on the custom page. If so, then I need to add the proper attributes to the anchor added to the top of the page: // Determine if we’re looking at the Headlines page. $attributes=”class=”””; if ( filter_input(INPUT_GET, ‘meta_value’) === ‘headline’ ) { $attributes=”class=”current aria-current=”page””; } After that, I need to actually add the Headlines view to the page. This will require the use of several functions: array_push for adding a new link to the list of $views sprintf for securely adding a new string add_query_arg for adding a set of custom query arguments to the current page. The next section of code will look like this: // Build the anchor for the ‘Headlines’ view and add it to $views. array_push( $views, sprintf( ‘<a href=”https://tommcfarlin.com/custom-link-all-posts-screen/%1$s” %2$s>%3$s <span class=”count”>(%4$s)</span> </a> ‘, add_query_arg([ ‘post_type’ => ‘post’, ‘post_status’ => ‘all’, ‘meta_value’ => ‘headline’, ], ‘edit.php’), $attributes __(‘Headlines’), count( /* … */ ); ); But I’m not done yet. Notice specifically that I’m making a call to count at the end of the function. This is so that I can properly display the number of posts that have this attribute. I’m going to write two helper functions for this then I’ll return back to the sizeof call. Here’s a helper function for finding the number of results that have the specified meta_key and meta_value that we have for this type of post. Notice that I’m using $wpdb to make a direct database call and that I’m specifically using prepare to make sure I do it safely. function get_headline_results() : array { global $wpdb; return $wpdb->get_results( // phpcs:ignore $wpdb->prepare( ” SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s “, ‘article_attribute’, ‘headline’ ), ARRAY_A ); } Notice that it returns all of the results (not just the number) because this value will be passed into another function momentarily. At this point, we could stop and simply look at the content that’s returned from the query but if we’re just concerned with the post post type, then we’ll need to account for that. Here’s one way to do it: function filter_posts_from_pages( array $results ) : array { $post_ids = array(); foreach ( $results as $result) { if ( ‘post’ === get_post_type( $result[‘post_type’] ) ) { $post_ids[] = $result[‘post_id’]; } } return $post_ids; } With that, we can return this value back to the count function

Continue reading