• Resolved CallMeAndy

    (@callmeandy)


    Hi guys,
    Dont know if anyone can help I am trying to populate a page with posts where a custom field “AssocBlogID” has a value = current page ID.

    Effectivly it is a page for a user to contain his own posts only. If there is a better way of doing it that fits with a Suffusion child theme I am all ears (well eyes – you know what I mean) My starting point was to copy and modify posts.php (a page of posts) so that I didnt break the theme. Obviously the following code is an extract.

    I am getting the correct value into $pageID and I have created one post to test the method, but the query is populating the page with a different post, that does not have AssocBlogID set. Can anybody point me to where I am going wrong – cheers.

    $pageID = $post->ID;
    
    $args = array(
        'meta_key' => 'AssocBlogID',
        'meta_value' => $pageID,
        'orderby' => 'date',
        'order' => 'DESC',
        'paged' => $paged,
    );
    //********************************End of mod
    
    $temp = $wp_query;  // assign original query to temp variable for later use
    //$wp_query = null; // Resetting this to null was causing a PHP Notice to pop up
    $wp_query = new WP_Query($args);

Viewing 2 replies - 1 through 2 (of 2 total)
  • Just a guess, but try using a different variable other than $wp_query to hold the query. Modify have_posts() and the_post() for the new variable.

    Thread Starter CallMeAndy

    (@callmeandy)

    So I delved a bit further, and picking at straws really I added some additional clauses not expecting any real difference to the product of the query but ultimately the outcome was a surprise.

    $args = array(
        //'meta_query' => array( 'key' => 'AssocBlogID',  'compare' => '==','value' => $pageID ),
        'meta_key' => 'AssocBlogID', 'compare' => '!=',
        'meta_value' => $pageID,
        'orderby' => 'date',
        'order' => 'DESC',
        'post_type'     =>    'post',
        'post_status' =>    'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1,
        'paged' => $paged,
    );

    Produces this query:
    SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish') AND ( (wp_postmeta.meta_key = 'AssocBlogID' AND CAST(wp_postmeta.meta_value AS CHAR) = '435') ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC

    Of course removing the caller_get_posts clause brings back in stickys, but these should not be there as they do not have the AssocBlogID meta set. I am obviously missing something else as well, as removing the caller_get_posts from the $args DOES NOT CHANGE THE SQL in wp_query.

    To say I was confounded at this point is an understatement.

    I hadnt realised that caller_get_posts has been depricated, Changing the line to ‘ignore_sticky_posts’ => 1, makes no difference however in gives the same results when included or excluded out still gives the same SQL

    Thats when I twigged and the results are confirmed in phpMyAdmin

    There must be some post processing going on to merge the results to the stickys –

    I had assumed that the way stickies worked was that: from a given set of results the sort order would always hold them at the top of the list. But what seems to be happening is that even when they do not form a part of a queries results they are being added back into the results. i.e All stiky posts are added to the output irrespective of the query unless specifically excluded.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Page Template: How to populate with crieteria in custom fields’ is closed to new replies.