ansonphong / postworld

Wordpress Theme Development Framework
GNU General Public License v2.0
7 stars 0 forks source link

pw_query() PHP Method - Creation #9

Closed ansonphong closed 11 years ago

ansonphong commented 11 years ago

Hi Haidy, There is a custom query function called pw_query() which is in the documentation. This function is required for most of the feed functions to operate, so it is important to work on it soon.

Do you have any questions about how the function operates? If so, please ask me.

You can find here it under pw_query() https://www.evernote.com/shard/s275/sh/7fd5bb62-0902-4050-9889-338f847d044c/5fe6f3da3510557663e8693251ac8557

We should prioritize this function over all other work this week until it's finished, so that the rest of the production can make use the function.

Thanks.

hmikhail commented 11 years ago

I'm working on this function now.

ansonphong commented 11 years ago

Great - I'm making a few additions to the documentation to help clarify the details. Don't hesitate to ask if you have any questions.

hmikhail commented 11 years ago

Phong, here is the status of this task until now.

I extended the wp_query class and modified some functions in it in order to join the wp_posts table with wp_postworld_post_meta table, still in progress and under testing. If I didn't fine further issues then the remianing part is to add postworld data with the posts data.

ansonphong commented 11 years ago

Thanks for the update. When do you think it will be ready to use?

hmikhail commented 11 years ago

I think it is now ready, I just want to make sure that the return type is implemented correctly

WP_Post : return the same type as returned from WP_Query. JSON : encode the output as json ARRAY_A : cast it to array

ansonphong commented 11 years ago

Yes exactly - I'll have a look at it and give it a spin.

Thanks, Phong

On Wednesday, September 18, 2013, hmikhail wrote:

I think it is now ready, I just want to make sure that the return type is implemented correctly

WP_Post : return the same type as returned from WP_Query. JSON : encode the output as json ARRAY_A : cast it to array

— Reply to this email directly or view it on GitHubhttps://github.com/phongmedia/postworld/issues/9#issuecomment-24668454 .

ansonphong commented 11 years ago

Hi Haidy, I'm trying to test PW_Query, and I'm getting this error :

WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 0,10' at line 1] SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts right join wp_postworld_post_meta on wp_posts.ID = wp_postworld_post_meta.post_id WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') order by DESC LIMIT 0,10

I also tried re-activating Postworld and get the same error. Is my database out of date, or what do you think is wrong?

hmikhail commented 11 years ago

I assume you didn't send orderby or posts_per_page parameters, I set default values for them now For the ease of testing, I created a class for the input parameters, try this for testing

$test_args = new pw_query_args(); $test_args->post_type="blog"; $test_args->post_type='page'; $test_args->post_format='video'; $test_args->post_class='feature'; $test_args->author='2'; $test_args->author_name='asun phong'; $test_args->year=2013; $test_args->month=8; $test_args->tax_query=array( 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => array( 'post-format-quote' ) ); $test_args->s='keyword'; $test_args->orderby="rank_score post_points rand comment_count"; $test_args->order='ASC'; $test_args->posts_per_page=-1;

also if you want to log to the output to a file (if you are using debug.log in wp) , I created a logger function use log_me(pw_query($test_args));

please let me know if you find more issues.

ansonphong commented 11 years ago

Great - I'll try this tomorrow when I'm back to work.

Would it work like this?

$test_args = array(
"post_type"=>"blog",
"post_format"=>"video",
"post_class"=>"feature"
...
)
log_me(pw_query($test_args));
hmikhail commented 11 years ago

yes this should work too.

ansonphong commented 11 years ago

I tried testing this function, and it seems like something is working, but it's not getting any results.

For example :

$pw_query_args = array(
    'post_type'     => 'post',
    'posts_per_page'=> 10,
    'fields'        =>  'ids',

    );
$pw_query_test = PW_Query($pw_query_args);
echo $pw_query_test;

PW_Query produces this :

{"query":{"orderby":"rand","post_type":"post","posts_per_page":10,"fields":"ids"},"query_vars":{"orderby":"rand","post_type":"post","posts_per_page":10,"fields":"ids","error":"","m":0,"p":0,"post_parent":"","subpost":"","subpost_id":"","attachment":"","attachment_id":0,"name":"","static":"","pagename":"","page_id":0,"second":"","minute":"","hour":"","day":0,"monthnum":0,"year":0,"w":0,"category_name":"","tag":"","cat":"","tag_id":"","author_name":"","feed":"","tb":"","paged":0,"comments_popup":"","meta_key":"","meta_value":"","preview":"","s":"","sentence":"","menu_order":"","categoryin":[],"categorynot_in":[],"categoryand":[],"postin":[],"postnot_in":[],"tagin":[],"tagnot_in":[],"tagand":[],"tag_slugin":[],"tag_slug__and":[],"post_parentin":[],"post_parent__not_in":[],"ignore_sticky_posts":false,"suppress_filters":false,"cache_results":true,"update_post_term_cache":true,"update_post_meta_cache":true,"nopaging":false,"comments_per_page":"50","no_found_rows":false,"order":"DESC"},"tax_query":{"queries":[],"relation":"AND"},"meta_query":{"queries":[],"relation":null},"request":"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts right join wp_postworld_post_meta on wp_posts.ID = wp_postworld_post_meta.post_id WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') order by RAND() DESC LIMIT 0,10","posts":[],"post_count":0,"current_post":-1,"in_the_loop":false,"comment_count":0,"current_comment":-1,"found_posts":0,"max_num_pages":0,"max_num_comment_pages":0,"is_single":false,"is_preview":false,"is_page":false,"is_archive":false,"is_date":false,"is_year":false,"is_month":false,"is_day":false,"is_time":false,"is_author":false,"is_category":false,"is_tag":false,"is_tax":false,"is_search":false,"is_feed":false,"is_comment_feed":false,"is_trackback":false,"is_home":true,"is_404":false,"is_comments_popup":false,"is_paged":false,"is_admin":false,"is_attachment":false,"is_singular":false,"is_robots":false,"is_posts_page":false,"is_post_type_archive":false,"query_vars_hash":"dadfa05a41775b5f7da51a255647395d","query_vars_changed":false,"thumbnails_cached":false}

Whereas, if I run the same query through WP_Query, it gives a different result :

{"query":{"orderby":"rand","post_type":"post","posts_per_page":10,"fields":"ids"},"query_vars":{"orderby":"rand","post_type":"post","posts_per_page":10,"fields":"ids","error":"","m":0,"p":0,"post_parent":"","subpost":"","subpost_id":"","attachment":"","attachment_id":0,"name":"","static":"","pagename":"","page_id":0,"second":"","minute":"","hour":"","day":0,"monthnum":0,"year":0,"w":0,"category_name":"","tag":"","cat":"","tag_id":"","author_name":"","feed":"","tb":"","paged":0,"comments_popup":"","meta_key":"","meta_value":"","preview":"","s":"","sentence":"","menu_order":"","categoryin":[],"categorynot_in":[],"categoryand":[],"postin":[],"postnot_in":[],"tagin":[],"tagnot_in":[],"tagand":[],"tag_slugin":[],"tag_slug__and":[],"post_parentin":[],"post_parent__not_in":[],"ignore_sticky_posts":false,"suppress_filters":false,"cache_results":true,"update_post_term_cache":true,"update_post_meta_cache":true,"nopaging":false,"comments_per_page":"50","no_found_rows":false,"order":"DESC"},"tax_query":{"queries":[],"relation":"AND"},"meta_query":{"queries":[],"relation":null},"request":"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY RAND() DESC LIMIT 0, 10","posts":["169853","1176","1337","161203","10111","1809","147535","56049","160540","171"],"post_count":10,"current_post":-1,"in_the_loop":false,"comment_count":0,"current_comment":-1,"found_posts":"5067","max_num_pages":507,"max_num_comment_pages":0,"is_single":false,"is_preview":false,"is_page":false,"is_archive":false,"is_date":false,"is_year":false,"is_month":false,"is_day":false,"is_time":false,"is_author":false,"is_category":false,"is_tag":false,"is_tax":false,"is_search":false,"is_feed":false,"is_comment_feed":false,"is_trackback":false,"is_home":true,"is_404":false,"is_comments_popup":false,"is_paged":false,"is_admin":false,"is_attachment":false,"is_singular":false,"is_robots":false,"is_posts_page":false,"is_post_type_archive":false,"query_vars_hash":"dadfa05a41775b5f7da51a255647395d","query_vars_changed":false,"thumbnails_cached":false}

The notable diference is that, WP_Query _$pw_querytest->posts actually returns the post IDs :

"posts":["169853","1176","1337","161203","10111","1809","147535","56049","160540","171"]

Whereas the PW_Query _$pw_querytest->posts returns this :

"posts":[]

Do you have any ideas why this is happening? I haven't been able to get results out of PW_Query yet.

PS- I changed the 'ids' only in the PW spec from 'id' >> to >> 'ids', though if I use either as the value of 'fields' it still doesn't produce a result.

Both queries should return the same results in this case. Am I missing anything?

Thanks.

hmikhail commented 11 years ago

Phong, I have two questions :

Are you using the database wp_postworld_a1 that you sent its credentials? Which version of wordpress are you using?

I see from your query_args that you didn't send orderby paramater but in the query vars object in the output it is set to rand "query_vars":{"orderby":"rand",...

$pw_query_args = array( 'post_type' => 'post', 'posts_per_page'=> 10, 'fields' => 'ids', );

According to wp_query documentation : http://codex.wordpress.org/Class_Reference/WP_Query, the default value for "orderby" is post_date, and in wp_query, we use this default value if the parameter is not sent.

Would you like us not to override this part if the parameter is not sent?

I added a php file for testing, with different input parameters and expected outputs from wp_query and pw_query, I used a copy from your database. You can download the file from here : https://app.box.com/s/ysfkh0maionagqku5j0s and pull the latest version of the code.

We can schedule a meeting if we need to discuss anything.

ansonphong commented 11 years ago

Great thanks Haidy, I'll give that a try. We can keep the defaults from WP_Query for ordering (post_date) - that's a good idea.

On Tuesday, September 24, 2013, hmikhail wrote:

Phong, I have two questions :

Are you using the database wp_postworld_a1 that you sent its credentials? Which version of wordpress are you using?

I see from your query_args that you didn't send orderby paramater but in the query vars object in the output it is set to rand "query_vars":{"orderby":"rand",...

$pw_query_args = array( 'post_type' => 'post', 'posts_per_page'=> 10, 'fields' => 'ids', );

According to wp_query documentation : http://codex.wordpress.org/Class_Reference/WP_Query, the default value for "orderby" is post_date, and in wp_query, we use this default value if the parameter is not sent.

Would you like us not to override this part if the parameter is not sent?

I added a php file for testing, with different input parameters and expected outputs from wp_query and pw_query, I used a copy from your database. You can download the file from here : https://app.box.com/s/ysfkh0maionagqku5j0s and pull the latest version of the code.

We can schedule a meeting if we need to discuss anything.

— Reply to this email directly or view it on GitHubhttps://github.com/phongmedia/postworld/issues/9#issuecomment-24999968 .

ansonphong commented 11 years ago

OK, so whatever it was in your latest push causes it to return values now, which is good.

The one thing which would be helpful is if we can get the 'return_format' parameter working, so that we can return the data as ARRAY_A, JSON or WP_POST -- this will make it more flexible in how we deal with the data.

hmikhail commented 11 years ago

Already done.

$pw_query_test = pw_query($pw_query_args,'ARRAY_A'); print_r($pw_query_test);

$pw_query_test = pw_query($pw_query_args,'JSON'); print_r($pw_query_test);

$pw_query_test = pw_query($pw_query_args,'WP_POST'); print_r($pw_querytest); -> this returns type PWَQuery object, I suggest changing the return_format to be PW_QUERY instead of WP_POST

ansonphong commented 11 years ago

Oh I see, I like how you implimented it. Yes we can change the value to return wp post object to 'PW_QUERY'

On Wednesday, September 25, 2013, hmikhail wrote:

Already done.

$pw_query_test = pw_query($pw_query_args,'ARRAY_A'); print_r($pw_query_test);

$pw_query_test = pw_query($pw_query_args,'JSON'); print_r($pw_query_test);

$pw_query_test = pw_query($pw_query_args,'WP_POST'); print_r($pw_querytest); -> this returns type PWَQuery object, I suggest changing the return_format to be PW_QUERY instead of WP_POST

— Reply to this email directly or view it on GitHubhttps://github.com/phongmedia/postworld/issues/9#issuecomment-25073674 .

ansonphong commented 11 years ago

Initially we had set the default method for return to be 'JSON' although in order to make it work more predictably like WP_Query, let's make the default value of $return_format : PW_QUERY

hmikhail commented 11 years ago

Done in commit: e2711f62bd4409da1319b37251cd237b6d67be9c