• Resolved fas.khan

    (@faskhan)


    Hello,

    I have somewhat strange problem.

    Within the code, I am trying to send variables to the query_posts and it shows the posts according to user’s input.

    Now, to the problem.

    I am passing a variable which is serialized and stored as “meta_value”; Now, I want to show posts according to that.

    $thisserial = 'a:3:{i:0;s:14:"American Samoa";i:1;s:7:"Andorra";i:2;s:6:"Angola";}';
    
    			$querystring = "&meta_value="."$thisserial"	;
    query_posts('category_name=some_category'.$querystring);

    Now, wordpress should search the meta_value as same as $thisserial and give me all the posts, but it’s not. There is some problem with the format of the string, some apostrophes, colons or something that it hampering wordpress to show me posts matching this criteria.

    Whenever the variable $thisserial is normal as something like “This Value”, it shows everything normally.

    Can I have any assistance?

    How can I pass this variable ?

    Many Many Thanks

Viewing 11 replies - 1 through 11 (of 11 total)
  • I wouldn’t expect what you have coded to work. Unless the documentation is wrong, query_posts does not support serialized variables:
    https://codex.www.ads-software.com/Template_Tags/query_posts#Custom_Field_Parameters

    If this was my problem, I doubt that I would find any way to do this except to deserialize the variable myself, and set up a loop to build OR clauses in an SQL SELECT statement. Then run the query with get_results or other functions described here:
    https://codex.www.ads-software.com/Function_Reference/wpdb_Class

    All of which requires some research into WordPress database tables and column names.

    The problem is that the edit post and query handle the strings differently. The edit post re-serializes the data while the query escapes the double quotes with slashes. At least that is what is happening on my test site.

    You can add this line after the query_posts to see the actual query:

    print_r($wp_query->request);

    The database values are re-serialized and stored without the escaping slashes. So, when you enter this:

    a:3:{i:0;s:14:"American Samoa";i:1;s:7:"Andorra";i:2;s:6:"Angola";}

    for a Custom Field value, the database contains this:

    s:67:"a:3:{i:0;s:14:"American Samoa";i:1;s:7:"Andorra";i:2;s:6:"Angola";}";

    and, when you enter the same string in the query, it looks like this:

    wp_postmeta.meta_value = 'a:3:{i:0;s:14:\"American Samoa\";i:1;s:7:\"Andorra\";i:2;s:6:\"Angola\";}'

    Thread Starter fas.khan

    (@faskhan)

    So it means, there is no way, I can achieve this ??

    As I said, you can build your own SQL SELECT query. It even works for external databases (which is what I use it for).

    Thread Starter fas.khan

    (@faskhan)

    But Adiant, that SQL query is not that easy to make, and I don’t know how they are doing it.

    Simply, for example, this is what I am doing.

    $querystr = "SELECT DISTINCT (ID), wposts . * , wpostmeta . * 
    
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->postmeta wpostmeta2, $wpdb->postmeta wpostmeta3, $wpdb->postmeta wpostmeta4, $wpdb->postmeta wpostmeta5, $wpdb->postmeta wpostmeta6
    
    WHERE wposts.ID = wpostmeta.post_id
    
    AND wpostmeta3.meta_key =  'focusarea'
    
    AND wpostmeta3.meta_value LIKE  '%".$topic."%'
    
    AND wpostmeta4.meta_key =  'activity'
    
    AND wpostmeta4.meta_value =  '".$activity."'
    
    AND wpostmeta5.meta_key =  'status'
    
    AND wpostmeta5.meta_value = '".$status."'
    
    AND wpostmeta6.meta_key =  'audience'
    
    AND wpostmeta6.meta_value LIKE '%".$audience."%'
    
    AND wposts.post_status =  'publish'
    
    AND wposts.post_type =  'post'
    
    GROUP BY wposts.post_title";

    But then I have to add the same table instances multiple times and the result is not efficient. It shows me multiple records, or some records which i dont expect to see …

    Where am I going wrong?

    Many Thanks.

    Thread Starter fas.khan

    (@faskhan)

    One question please.

    the expression print_r($wp_query->request); gives the query we make through WP_Query.

    Is there any way I can see the query made by wordpress through get_posts or query_posts?

    Many Thanks,

    You can put the print_r statement just after get_header() in your theme. That will print the query that WP used to call the template.

    If you know the structure of the data that you put in the meta_value, just turn it into a comma separated list. That should not get serialized. Then construct the same value for your query.

    Thread Starter fas.khan

    (@faskhan)

    Thanks a lot for your reply …

    But after I put it in get_header(), I am still unable to see the query …
    any other suggestion ?

    The code probably should look like this:

    get_header(); ?>

    Change it to this:

    get_header(); print_r($wp_query->request); ?>

    Thread Starter fas.khan

    (@faskhan)

    Hi vtxyzzy …

    it shows me a query something like this
    SELECT aqoposts.* FROM aqoposts JOIN aqoicl_translations t ON aqoposts.ID = t.element_id AND t.element_type IN ('post_post','post_page') JOIN aqoicl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1 AND (aqoposts.ID = '20') AND aqoposts.post_type = 'page' AND t.language_code='en' ORDER BY aqoposts.post_date DESC

    I was hoping it will show me the query of the posts with multiple custom fields sorting as I am using this in my code :-
    $queryObject = get_posts('category_name=campaigns'.$querystring);

    where $querystring contains list of variables selected by the user (e.g. meta_key=’ddd’&meta_value=’yyy’&meta_key=’dtt’&meta_value=’yyre’&meta_key=’xxd’&meta_value=’eee’) ….

    I just want that it shows the code, it generates for “get_posts(…)”

    Any Idea please?

    Sorry, I misunderstood. Put the print_r just after your get_posts.

    Also, I noticed that your $querystring doesn’t seem to start with an ampersand, and you concatenate to it like this:

    'category_name=campaigns'.$querystring

    I wonder if that should be this:

    'category_name=campaigns&'.$querystring

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘query_posts passing a variable’ is closed to new replies.