Software Engineering in WordPress, PHP, and Backend Development

Search: “wp_query” (Page 2 of 10)

We found 48 results for your search.

Using WP_Query To Get Posts From Last Week

One of the most powerful aspects of the WordPress API is WP_Query as it allows us to retrieve a wide variety of content through a relatively simple interface. That is, it allows us to retrieve complicated datasets ranging from custom post types, post status, date ranges, taxonomies, meta data, categories, and so on all […]

Using WP_Query and Multiple Meta Keys

When it comes to searching the WordPress database, there’s probably no better API than WP_Query. That doesn’t mean it’s not without it’s limitations, though (only which will be improved over time, I’m sure). For example, consider the case where you have two types of meta data that are completely unrelated and you need to […]

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

[…] 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( ‘%3$s (%4$s) ‘, add_query_arg(, ‘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_ids = $result; } } return $post_ids; } With that, we can return this value back to the count function above. The final version of the block that we started above should look something like this: // Build the anchor for the ‘Headlines’ and add it to $views. array_push( $views, sprintf( ‘%3$s (%4$s)’, add_query_arg( array( ‘post_type’ => ‘post’, ‘post_status’ => ‘all’, ‘meta_value’ => ‘headlines’, // phpcs:ignore ), ‘edit.php’ ), $attributes, __( ‘Headlines’ ), count( filter_posts_from_pages( get_headline_results() ) ) ) ); Which means the complete version of the function for adding a new headline looks something like this: add_action( ‘views_edit-post’, function ( array $views ) { // 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”‘; } // Build the anchor for the ‘Headlines’ and add it to $views. array_push( $views, sprintf( ‘%3$s (%4$s)’, add_query_arg( array( ‘post_type’ => ‘post’, ‘post_status’ => ‘all’, ‘meta_value’ => ‘headline’, // phpcs:ignore ), ‘edit.php’ ), $attributes, __( ‘Headlines’ ), count( filter_posts_from_pages( get_headline_results() ) ) ) ); return $views; } ); And, as mentioned from the outset, […]

Using WP_Query To Search For a Person

One of the most powerful features of WP_Query is that it allows for us to create a type of mini-search engine within the context of our WordPress projects. No, this isn’t as sophisticated as something or someone who’s actually in the search business, but you can create some pretty elaborate queries using WP_Query. Conversely, […]

« Older posts Newer posts »

© 2024 Tom McFarlin

Theme by Anders NorenUp ↑