• Hi All,

    I’m building a plugin to provide post tagging for my WP blog (yes, I’m aware plugins already exist for this, I’m doing this more for my own plugin learning curve), and I’m storing the tags via custom fields (ie in wp_postmeta).

    I’ve created a page based on a template in which I intend to display all posts with a given tag (so, you would click on a tag link in a post, and it should take you to this page with the tag name in the URL).

    I’m trying to work out if there’s a way to provide a custom SELECT query to The Loop – something like:

    SELECT p.* FROM wp_posts p, wp_postmeta pm WHERE p.ID = pm.post_id and meta_key='tags' and meta_value='value_of_tag'

    I’ve spent a couple of hours trying to find something to tell if this is possible or not in the docs, and the few other tag plugins I’ve found just link to technorati, not to a page where all entries with that tag are displayed.

    Any help appreciated!

    Much warmth,

    planetthoughtful

Viewing 15 replies - 16 through 30 (of 36 total)
  • Thanks Kaf!

    K one more question Kaf, I did what you said and it is working beutifully!

    Now I have another instance of the loop on the same page (a top 10 list) which will do the same exact thing except instead of the current category, I want it to query the parent category of the current.

    Is there a way to do it more locally so it doesnt affect the main one?

    For instance put something like:
    ("category=" . $cat->cat_ID . "&numberposts=10")

    but I don’t know where to put that in:
    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    Well, $cat->cat_ID would hold no value, since $cat is a basic PHP variable holding a single value (the category ID).

    If you want to collect the category parent ID for the current category, you can try this:

    <?php $cat_object = $wp_query->get_queried_object(); ?>

    With that, $cat_object->category_parent will hold what you’re looking for.

    like this?


    <?php $cat_object = $wp_query->get_queried_object("category=" . $cat_object->category_parent . "&numberposts=10"); ?>
    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    That doesn’t seem to work but i am pretty sure thats not right. Is there something I need to change in there?

    <?php
    $cat_object = $wp_query->get_queried_object();
    $pageposts = new WP_Query("category=" . $cat_object->category_parent . "&numberposts=10");
    ?>
    <?php if ($pageposts): ?>
    <?php foreach ($pageposts as $post): ?>
    <?php setup_postdata($post); ?>

    Actually, looking over that I think you’ll run into problems. So try a loop like:

    <?php
    $cat_object = $wp_query->get_queried_object();
    if ($cat_object->category_parent) :
    $pageposts = new WP_Query("category=" . $cat_object->category_parent . "&numberposts=10");

    while ($pageposts->have_posts() ) : $pageposts->the_post(); ?>

    ~template tags ‘n stuff go here~

    <?php endwhile; endif; ?>

    Kaf, Does this go before, after, or inside the other loop?

    Right now I put it after to test it, and its not returning the right parent category info. i need the parent of the subcategory I am viewing.

    I can currently get the title of the desired parent by using:
    <?php $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name; ?>

    Do i need to apply something similair for that for this loop?

    Does this go before, after, or inside the other loop?

    It *is* a loop, meaning what it is you’re using at this point — “I have another instance of the loop on the same page (a top 10 list) which will do the same exact thing except instead of the current category, I want it to query the parent category of the current.” — you would use the above code there as your loop; making sure to replace ~template tags ‘n stuff go here~ with something useful, of course.

    I (again) tested locally the code I gave above, and for me it is displaying the posts from the parent category of any child category.

    Finally, I don’t know if it’s an issue here, but $cat is a global WordPress var and reusing it is considered, at the very least, bad form. I’d recommend a change to your code, like such:

    <?php $thiscat = get_the_category(); $thiscat = $thiscat[0]; echo $thiscat->cat_name; ?>

    hmm, thats what I thought. Its so weird though because the posts it is displaying is from a totally unrelated category that has absolutely nothing to do with the current page. Maybe it has something to do with my query:

    <?php
    $topposts = $wpdb->get_results("SELECT $wpdb->posts.*, $wpdb->postmeta.meta_key FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id LEFT JOIN $wpdb->post2cat ON $wpdb->post2cat.post_id = $wpdb->posts.ID WHERE $wpdb->post2cat.category_id = '$cat' AND $wpdb->postmeta.meta_key = 'ratings_score' AND $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_date < NOW() ORDER BY $wpdb->postmeta.meta_value DESC", OBJECT);
    ?>
    <div id="top10_list">
    <h2>Top 10
    <?php $thiscat = get_the_category(); $thiscat = $thiscat[0]; echo $thiscat->cat_name; ?> Albums</h2>
    <?php
    $cat_object = $wp_query->get_queried_object();
    if ($cat_object->category_parent) :
    $topposts = new WP_Query("category=" . $cat_object->category_parent . "&numberposts=10");
    while ($topposts->have_posts() ) : $topposts->the_post(); ?>
    <!--content-->
    <?php endwhile; endif; ?>
    </div>

    Oh boy.

    The first, but not most important, issue in your code is: get_the_category() is an ‘in The Loop’ function, in that it gets its values from the posts, and not the category query, object. Using it as you are might cause unexpected results. You could work strictly off $cat_object from my code and call the category’s name from that (echo $cat_object->cat_name), or display the current category’s title with the single_cat_title() template tag.

    Now the most important… my stuff from above is screwing you up. Use of category and numberposts as arguments to WP_Query won’t work, as they are specific to get_posts() (the correct ones are cat and showposts, respectively). I’m not sure why I didn’t see this as I was testing locally, etc. But no excuses, and sorry for the misleading code.

    With that, this version of your second $topposts line *will* work for you:

    $topposts = new WP_Query("cat=" . $cat_object->category_parent . "&showposts=10");

    Awesome! there we go. Except it is not ordering them according to my query, by rating.

    Also, if this is the parent category I want it to show the top 10 of itself but since that code is for parent, it shows nothing. Is there an if statement we can write or something?

    thanks again for all your help Kaf!

    This might be an easier solution. Due to the layout it looks like I am going to have to run 2 querys anyway so is there a wp global variable for parent category? because if there is, I can just change $wpdb->post2cat.category_id = '$cat'

    would stil need some kind of if though in case it is the parent

    Ah, missed the part about ordering by rating. For that you would have to go with your original query line and nix the WP_Query call (as well as use of the standard Loop). That sort of sucks, but not completely since it will still work.

    Anyway, here it is wrapped up; it will switch between parent (when on a child) cat and current (when on a parent) cat:

    http://wordpress.pastebin.ca/246228

    Note this outputs the current category name using single_cat_title(). If this is not expected behavior (though it matches the original code) and you want the name of the “top list” category, replace the single_cat_title() template tag with this complicated bit:

    <?php echo $GLOBALS['wp_object_cache']->cache['category'][$querycat]->cat_name; ?>

    If that won’t work (not all setups give up the $GLOBALS array), a query will solve things:

    <?php echo $wpdb->get_var("SELECT cat_name FROM $wpdb->categories WHERE cat_ID = '$querycat'"); ?>

    EDIT: Posted before seeing the reply just above it.

    This might be an easier solution.

    Ok, let’s start over and have you provide a blow-by-blow of everything that is truly needed here. Because at this point I’ve lost the plot.

    *That’s what happens when you aren’t writing the novel to begin with*….

Viewing 15 replies - 16 through 30 (of 36 total)
  • The topic ‘Any way to provide custom select query to the loop?’ is closed to new replies.