Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Query posts with custom field whatever it's null WordPress

  • REFUNDED

Hello, ive tried many ways to make it work right but..?


All i neeed making sort posts by downloads or views etc..


but some posts has null custom fields and it doesn't appear




global $wp_query;
$args = array_merge( $wp_query->query, array( 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => 'download_counter' ) );

query_posts( $args );







if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<? include(TEMPLATEPATH.'/templates/misc/thumb.php'); ?>
<? endwhile;endif;
wp_reset_query();




if(function_exists('wp_paginate')) { wp_paginate(5,5);





i don't want forech or any other function to be working... just

if ( have_posts() ) : while ( have_posts() ) : the_post();

Answers (3)

2014-11-02

Bob answers:

You mean when you do not add short parameter in argument you get all posts but if you add short related arguments it doesn't show the posts?

can you share url with us? or is are you working in localhost?


wassimo Ghaleb comments:

it works well shows all posts but if there's any post has empty custom field it doesnt show

it only shows posts who has custom field value


Bob comments:

What I am asking is.....

What happens if we do not modify global query? then it show all posts? with empty custom field also?

is there any condition in your thumb.php file?

If because of add the custom meta orderby posts are removed/not visible then this link will become helpful to you.
[[LINK href="https://core.trac.wordpress.org/ticket/19653"]]https://core.trac.wordpress.org/ticket/19653
[[/LINK]]


wassimo Ghaleb comments:

ive tried to put excluse_empty but it doesn't work


thumb.php is for post details ( post thumbnail and post name views count download count ... )



how you gona modify modify global query?


Bob comments:

That link has 2 Solutions 1 Is19653.diff link. Which says modifications into core files n then use exclude_empty



the other one is to use to filter. Read above link again.

2014-11-03

Hariprasad Vijayan answers:

Hi,

Here is a solution for this problem, Follow the two steps.

1. update meta_key "download_counter" on postmeta with 0(zero) for the post doesn't have the meta_value/ have NULL from phpmyadmin using custom sql query or with a custom php script( for one time use).

2. then create a script for adding/updating postmeta to set meta_key "download_counter" with 0 when publishing a post.
Code would be like this,

add_action('publish_post', 'add_custom_field_automatically');
function add_custom_field_automatically($post_ID) {
global $wpdb;
if(!wp_is_post_revision($post_ID)) {
add_post_meta($post_ID, 'download_counter', '0', true);
}
}

Let me know if you need my help.

Thanks.


wassimo Ghaleb comments:

my website had around 50,0000 posts around 20% of them are null
so can't modify them all

2014-11-03

Arumai Prakasam answers:



Check this answer...

http://stackoverflow.com/questions/11314896/wordpress-query-order-by-custom-field-missing-posts