• Resolved kaushalacharya

    (@kaushalacharya)


    Hi All,

    Here is my page https://www.godfathercoupons.com/index.php?search-class
    It gives output with following query :-

    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’) LIMIT 0, 10

    I just want to add order by in that query order by post_title desc
    How can I do that? because I do not know from where the query is getting generated.

    I used the following :-
    global $query_string;
    query_posts( $query_string . ‘orderby=post_title&order=ASC’ );

    & then if I am echoing a query like echo $wp_query->request;
    It is giving the same query as previous no change.

    Thanks
    Kaushal

    [mod note: don’t bump your topic, it is not permitted on these forums]

Viewing 15 replies - 1 through 15 (of 63 total)
  • Thread Starter kaushalacharya

    (@kaushalacharya)

    Can any one please help?

    Thanks
    Kaushal

    The simplest way I run WordPress Sql statment is with:

    $result = $wpdb->get_results( “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’) ORDER BY ‘post_title'”);

    If you have any troubles contact me.

    S.

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Thanks Sobermedia for your answer ??

    Now the thing is my file is starting like this (on the top most part)
    <?php
    get_header();

    // STORES DESCRIPTION
    $term = get_term_by( ‘slug’, get_query_var( ‘term’ ), get_query_var( ‘taxonomy’ ) );
    if(isset($term->taxonomy) && $term->taxonomy == “store”){
    $GLOBALS[‘catText’] = wpautop(stripslashes($PPTDesign->StoreExtras($term->term_id,”text”)));
    $GLOBALS[‘catImage’] = $PPTDesign->StoreExtras($term->term_id,”image”,1);
    }

    echo $wp_query->request;

    $wp_query = new WP_Query();
    $args = array(
    ‘orderby’ => ‘post_title’,
    ‘order’ => ‘DESC’
    );
    $wp_query->query($args);

    echo “”.$wp_query->request;
    ?>

    See I am echoing 2 times echo $wp_query->request; but both the time query remains same. So my question is query is getting generated somewhere else so your solution will work here? I mean rather than writing the following :-
    $wp_query = new WP_Query();
    $args = array(
    ‘orderby’ => ‘post_title’,
    ‘order’ => ‘DESC’
    );

    Will your solution work? I mean will your solution replace the existing query?
    My link is here https://www.godfathercoupons.com/index.php?search-class

    Thanks again Sobermedia ??

    If you backup the file and then try the solution and let me know.

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sobermedia thanks for your help ??
    Now the thing is I am writing a little file code here to show you what exactly I did.
    [code]
    <?php
    get_header();
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    if(isset($term->taxonomy) && $term->taxonomy == "store")
    {
    $GLOBALS['catText']=wpautop(stripslashes($PPTDesign->StoreExtras($term->term_id,"text")));
    $GLOBALS['catImage']=$PPTDesign->StoreExtras($term->term_id,"image",1);
    }

    echo $wp_query->request;
    $result = $wpdb->get_results("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') ORDER BY post_title desc");
    echo "
    ".$wp_query->request;
    ?>
    [/code]

    Now Sober….my ultimate aim is to change the query which is echoed by this statement $wp_query->request;

    Because if this statement will echo the required query then all the problem will be solved automatically ??
    & just in case that this file _gallerypage.php is located at the following location :-
    /public_html/wp-content/themes/couponpress/template_couponpress

    May be this can help ??

    Now can you please tell me what to do? ??

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sober…

    When $wp_query->request this statement will echo the following :-
    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’) ORDER BY post_title desc

    then automatically the results below will be sorted ??
    Here is the link godfathercoupons.com/?search-class

    So any idea how to replace/change existing query?

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sober…

    When $wp_query->request this statement will echo the following :-
    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’) ORDER BY post_title desc

    then automatically the results below will be sorted ??
    Here is the link godfathercoupons.com/?search-class

    So any idea how to replace/change existing query?

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Sorry for reposting again ?? I refreshed the page but could not see my lat post so I again click on submit ??

    Kaushal

    I was giving you an example of how I use custom SQL queries in wordpress, it seems this is a plugin issues, what plugin are you using?

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sober…

    We are using Coupon Press Theme. But one more thing I would like to tell you that first _gallerypage.php this file runs ok? then class_design.php this file runs & in that $GLOBALS[‘query_data’] this statement contains the resultant data then foreach loop is there so each post is coming through that loop.

    Now see these 2 links
    1) https://www.godfathercoupons.com/index.php?cat=541&orderby=title&order=desc
    2) https://www.godfathercoupons.com/?search-class (this is when you click on All Coupons)

    Now query in 1st one’s case will run perfactly. but in 2nd case order by is not coming. Location of the query is same where I am eachoing.

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    So that I thought whether query can be changed run time?
    because location of query is same so by any means I will manage which link is running. But after that what how to change the query ??

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sober…

    I tried almost all the things but ??
    ?? so that I came to this forum.

    You helped a lot sober ?? thanks for that ??

    Thanks
    Kaushal

    Kaushal

    The only way to override the WP_Query is to do the following but I have seen you have done this:

    $query_args = array(
    ‘meta_key’ => ‘post_title’,
    ‘orderby’ => ‘meta_value’,
    ‘order’ => ‘DESC’
    );
    $wp_query = new WP_Query($query_args);

    You need the orderby to be a meta_value and NOT the field name see orderby parameters

    Try the above and let me know please.

    S.

    Thread Starter kaushalacharya

    (@kaushalacharya)

    Dear Sober…

    Nice one ?? Now it getting affected but ??
    Please see the link https://www.godfathercoupons.com/?search-class

    Sober please see that top line Showing results for with 15 coupons
    but it is showing only 10 & no 2nd page link which was previously there
    ??

    Thanks
    Kaushal

    Thread Starter kaushalacharya

    (@kaushalacharya)

    And Sober now the query has been changed then why the results are not coming in descending sort? ??

    So what I think is just to add order by post title in desc. in the following query itself only ??

    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’) LIMIT 0, 10

    Is that possible?

    Thanks
    Kaushal

Viewing 15 replies - 1 through 15 (of 63 total)
  • The topic ‘Add where clause in query run time’ is closed to new replies.