Skip to content

Commit

Permalink
Merge branch 'hotfix/filter-recommended-posts' into stage
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveJonesDev committed Oct 25, 2023
2 parents 58383d9 + 94c5534 commit 47315e4
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,17 @@ function eqd_template_hierarchy( $template ) {
*
* @return array The modified query arguments.
*/
function custom_acf_post_object_query( $args, $field, $post_id ) {
function npp_custom_acf_post_object_query( $args, $field, $post_id ) {
// Check if the field being queried is named 'recommended_posts'.
if ($field['name'] === 'recommended_posts') {
if ( $field['name'] === 'recommended_posts' ) {
// Modify the query to search for post titles only.
$args['post_type'] = 'post';
$args['s'] = ''; // Clear any previous search query.
//$args['s'] = ''; // Clear any previous search query.
$args['search_columns'] = array('post_title');
}
return $args;
}
add_filter('acf/fields/post_object/query', 'custom_acf_post_object_query', 10, 3);
add_filter( 'acf/fields/post_object/query', 'npp_custom_acf_post_object_query', 10, 3 );

/**
* Filters the query arguments for the 'recommendedfeatured_posts' ACF Post Object field.
Expand Down Expand Up @@ -345,3 +345,31 @@ function npp_filter_post_object_query( $args, $field, $post_id ) {
return $args;
}
add_filter( 'acf/fields/post_object/query', 'npp_filter_post_object_query', 10, 3 );

function modify_post_object_query( $args, $field, $post_id ) {
// Check if the parent block is 'acf/recommended-posts-block'
if ( isset($field['parent']) && $field['parent'] == 'block_acf/recommended-posts-block' ) {
// If there's a search term, modify the query to search by title only
if( isset($args['s']) ) {
// Set search term to a variable
$search_term = $args['s'];

// Modify the query
unset($args['s']); // Remove default search
$args['post_title_like'] = $search_term; // Add title search
}
}

// Return the modified arguments
return $args;
}
//add_filter('acf/fields/post_object/query', 'modify_post_object_query', 10, 3);

function title_like_posts_where( $where, $wp_query ) {
global $wpdb;
if ( $post_title_like = $wp_query->get('post_title_like') ) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'' . esc_sql( $wpdb->esc_like( $post_title_like ) ) . '%\'';
}
return $where;
}
//add_filter('posts_where', 'title_like_posts_where', 10, 2);

0 comments on commit 47315e4

Please sign in to comment.