Skip to main content
deleted 1394 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31

The code below is suppose to display all posts with "featured_listing" meta key above all of the standard posts.

When the below code is in the functions.php, it hides all posts without "featured_listing" meta. Instead of showing them below the featured listings.

function custom_special_sort( $query ) {
     if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    // if is this the main query and is this post type of business
    if ( (is_post_type_archive('business') ) || (is_tax ('location') ) ) {

        // order results by the meta_key 'featured_listing'
       $query->set( 'meta_key', 'featured_listing' );
        $query->set( 'orderby', 'featured_listing' );
        $query->set( 'order', 'DESC' );
        
    }
}
add_action( 'pre_get_posts', 'custom_special_sort' );

I thought I had solved the problem with the following code :

add_filter('posts_join', 'custom_special_sort_join' );
function custom_special_sort_join($wp_join)
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
        global $wpdb;
        $wp_join .= " LEFT JOIN (
                SELECT post_id, meta_value as date_due
                FROM $wpdb->postmeta
                WHERE meta_key =  'featured_listing' ) AS DD
                ON $wpdb->posts.ID = DD.post_id ";
    }
    return ($wp_join);
}



add_filter('posts_orderby', 'custom_special_sort_order' );
function custom_special_sort_order( $orderby )
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
            $orderby = " STR_TO_DATE(DD.date_due,'%m/%d/%Y') DESC ";
    }
    return $orderby;
}

but upon adding more posts featured_listings I realised it wasn't sorting the post_meta values at all.

Is it because the post_meta "featured_listing" is on every post regardless of the value being empty or filled ? I want to order based on that post_meta being filled with "featured_listing", If empty sort normally.

I've now gone through so many different posts, I feel like im probably looking right at the solution but have gone blind to it.

The code below is suppose to display all posts with "featured_listing" meta key above all of the standard posts.

When the below code is in the functions.php, it hides all posts without "featured_listing" meta. Instead of showing them below the featured listings.

function custom_special_sort( $query ) {
     if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    // if is this the main query and is this post type of business
    if ( (is_post_type_archive('business') ) || (is_tax ('location') ) ) {

        // order results by the meta_key 'featured_listing'
       $query->set( 'meta_key', 'featured_listing' );
        $query->set( 'orderby', 'featured_listing' );
        $query->set( 'order', 'DESC' );
        
    }
}
add_action( 'pre_get_posts', 'custom_special_sort' );

I thought I had solved the problem with the following code :

add_filter('posts_join', 'custom_special_sort_join' );
function custom_special_sort_join($wp_join)
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
        global $wpdb;
        $wp_join .= " LEFT JOIN (
                SELECT post_id, meta_value as date_due
                FROM $wpdb->postmeta
                WHERE meta_key =  'featured_listing' ) AS DD
                ON $wpdb->posts.ID = DD.post_id ";
    }
    return ($wp_join);
}



add_filter('posts_orderby', 'custom_special_sort_order' );
function custom_special_sort_order( $orderby )
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
            $orderby = " STR_TO_DATE(DD.date_due,'%m/%d/%Y') DESC ";
    }
    return $orderby;
}

but upon adding more posts featured_listings I realised it wasn't sorting the post_meta values at all.

Is it because the post_meta "featured_listing" is on every post regardless of the value being empty or filled ? I want to order based on that post_meta being filled with "featured_listing", If empty sort normally.

I've now gone through so many different posts, I feel like im probably looking right at the solution but have gone blind to it.

The code below is suppose to display all posts with "featured_listing" meta key above all of the standard posts.

When the below code is in the functions.php, it hides all posts without "featured_listing" meta. Instead of showing them below the featured listings.

function custom_special_sort( $query ) {
     if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }
    // if is this the main query and is this post type of business
    if ( (is_post_type_archive('business') ) || (is_tax ('location') ) ) {

        // order results by the meta_key 'featured_listing'
       $query->set( 'meta_key', 'featured_listing' );
        $query->set( 'orderby', 'featured_listing' );
        $query->set( 'order', 'DESC' );
        
    }
}
add_action( 'pre_get_posts', 'custom_special_sort' );
added 982 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31

When the below code is in the functions.php, it hides all posts without "featured_listing" meta.

I can't work out why its not working. Help would be appreciated Instead of showing them below the featured listings.

I suspect this could be similar to this mentioned on stackoverflow but I'm really not surethought I had solved the problem with the following code :

the query is doing an inner join on the posts and postmeta tables, which is > why posts that don't have that particular post meta entry aren't being returned by the query. What you want is a left join

add_filter('posts_join', 'custom_special_sort_join' );
function custom_special_sort_join($wp_join)
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
        global $wpdb;
        $wp_join .= " LEFT JOIN (
                SELECT post_id, meta_value as date_due
                FROM $wpdb->postmeta
                WHERE meta_key =  'featured_listing' ) AS DD
                ON $wpdb->posts.ID = DD.post_id ";
    }
    return ($wp_join);
}



add_filter('posts_orderby', 'custom_special_sort_order' );
function custom_special_sort_order( $orderby )
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
            $orderby = " STR_TO_DATE(DD.date_due,'%m/%d/%Y') DESC ";
    }
    return $orderby;
}

https://stackoverflow.com/questions/30361635/wp-archives-order-by-custom-meta-key but upon adding more posts featured_listings I realised it wasn't sorting the post_meta values at all.

Is it because the post_meta "featured_listing" is on every post regardless of the value being empty or filled ? I want to order based on that post_meta being filled with "featured_listing", If empty sort normally.

I've now gone through so many different posts, I feel like im probably looking right at the solution but have gone blind to it.

When the below code is in the functions.php, it hides all posts without "featured_listing" meta.

I can't work out why its not working. Help would be appreciated.

I suspect this could be similar to this mentioned on stackoverflow but I'm really not sure :

the query is doing an inner join on the posts and postmeta tables, which is > why posts that don't have that particular post meta entry aren't being returned by the query. What you want is a left join

https://stackoverflow.com/questions/30361635/wp-archives-order-by-custom-meta-key

When the below code is in the functions.php, it hides all posts without "featured_listing" meta. Instead of showing them below the featured listings.

I thought I had solved the problem with the following code :

add_filter('posts_join', 'custom_special_sort_join' );
function custom_special_sort_join($wp_join)
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
        global $wpdb;
        $wp_join .= " LEFT JOIN (
                SELECT post_id, meta_value as date_due
                FROM $wpdb->postmeta
                WHERE meta_key =  'featured_listing' ) AS DD
                ON $wpdb->posts.ID = DD.post_id ";
    }
    return ($wp_join);
}



add_filter('posts_orderby', 'custom_special_sort_order' );
function custom_special_sort_order( $orderby )
{
    if(is_post_type_archive('business') || is_tax ('location') || (is_admin() && $_GET['post_type'] == 'business')) {
            $orderby = " STR_TO_DATE(DD.date_due,'%m/%d/%Y') DESC ";
    }
    return $orderby;
}

but upon adding more posts featured_listings I realised it wasn't sorting the post_meta values at all.

Is it because the post_meta "featured_listing" is on every post regardless of the value being empty or filled ? I want to order based on that post_meta being filled with "featured_listing", If empty sort normally.

I've now gone through so many different posts, I feel like im probably looking right at the solution but have gone blind to it.

Rollback to Revision 5
Source Link
Randomer11
  • 416
  • 11
  • 31

EDIT ===================================

Following Jans comment below about a duplicate query, the only other customisations I have are the following. But removing them doesnt fix the problem so im a little stuck as to what the problemsuspect this could be. similar to this mentioned on stackoverflow but I'm really not sure :

// Custom field Searching - Join posts and postmeta tables
 function custom_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'custom_search_join' );

// Custom field Searching - Modify the search query with posts_where
function custom_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'custom_search_where' );

// Custom field Searching - Prevent duplicates
function custom_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
    wp_reset_postdata();
}
add_filter( 'posts_distinct', 'custom_search_distinct' );

the query is doing an inner join on the posts and postmeta tables, which is > why posts that don't have that particular post meta entry aren't being returned by the query. What you want is a left join

// Make sure custom post type can be searched.      
function custom_cpt_search( $query ) {
       if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
            $query->set('post_type', array('business'));
        }
        return $query;  
    };
    wp_reset_postdata();
    
add_filter('pre_get_posts', 'custom_cpt_search');

https://stackoverflow.com/questions/30361635/wp-archives-order-by-custom-meta-key

EDIT ===================================

Following Jans comment below about a duplicate query, the only other customisations I have are the following. But removing them doesnt fix the problem so im a little stuck as to what the problem could be.

// Custom field Searching - Join posts and postmeta tables
 function custom_search_join( $join ) {
    global $wpdb;

    if ( is_search() ) {    
        $join .=' LEFT JOIN '.$wpdb->postmeta. ' cfmeta ON '. $wpdb->posts . '.ID = cfmeta.post_id ';
    }

    return $join;
}
add_filter('posts_join', 'custom_search_join' );

// Custom field Searching - Modify the search query with posts_where
function custom_search_where( $where ) {
    global $pagenow, $wpdb;

    if ( is_search() ) {
        $where = preg_replace(
            "/\(\s*".$wpdb->posts.".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
            "(".$wpdb->posts.".post_title LIKE $1) OR (cfmeta.meta_value LIKE $1)", $where );
    }

    return $where;
}
add_filter( 'posts_where', 'custom_search_where' );

// Custom field Searching - Prevent duplicates
function custom_search_distinct( $where ) {
    global $wpdb;

    if ( is_search() ) {
        return "DISTINCT";
    }

    return $where;
    wp_reset_postdata();
}
add_filter( 'posts_distinct', 'custom_search_distinct' );
// Make sure custom post type can be searched.      
function custom_cpt_search( $query ) {
       if ( is_search() && $query->is_main_query() && $query->get( 's' ) ){
            $query->set('post_type', array('business'));
        }
        return $query;  
    };
    wp_reset_postdata();
    
add_filter('pre_get_posts', 'custom_cpt_search');

I suspect this could be similar to this mentioned on stackoverflow but I'm really not sure :

the query is doing an inner join on the posts and postmeta tables, which is > why posts that don't have that particular post meta entry aren't being returned by the query. What you want is a left join

https://stackoverflow.com/questions/30361635/wp-archives-order-by-custom-meta-key

added 1493 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31
Loading
deleted 62 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31
Loading
edited title
Link
Randomer11
  • 416
  • 11
  • 31
Loading
deleted 993 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31
Loading
added 1109 characters in body
Source Link
Randomer11
  • 416
  • 11
  • 31
Loading
Source Link
Randomer11
  • 416
  • 11
  • 31
Loading