• I’m building a quarterly literary journal. Unfortunately, I’m not a seasoned PHP guy or familiar enough with the WordPress codex to figure out a simple thing like this. I’ve added issue management, and can get the current ID of the journal by doing this:

    [code]$categories = get_categories('child_of=33&number=1&order=desc');
    foreach ($categories as $category) {
    $currentissue1 = $category->cat_ID;
    $currentissue2 = $category->cat_name;
    }
    [/code]
    (33 is the ID of the issue category, which is managed, so doing this will give me the current issue)

    But then I’m lost on how to cast the array variable as a string to add it to a query loop.

    So my question is –
    Is there a built-in function for this, and how would I use it?

    If not, how do I query with the information returned here? Ideally, having an actual variable like $current_issue_id to plug in to queries would be great!

Viewing 9 replies - 1 through 9 (of 9 total)
  • Do you want to set up a new query that fetches posts that are assigned one of the child categories you’ve already pulled with get_categories()?

    $categories = get_categories( 'child_of=33&number=1&order=desc' );
    foreach ($categories as $category) {
        $child_category_ids[] = $category->cat_ID;
    }
    
    $args = array(
        'category__in' => $child_category_ids;
    );
    
    $my_query = WP_Query( $args );

    Edit: Actually looking at your get_categories() arguments, do you mean to limit the number of returned categories to one? Makes the subsequent foreach loop a bit redundant…

    Thread Starter johnnywhee

    (@johnnywhee)

    Thanks for your response.

    I have an “issues” category that is managed, so that all its subcategories for a particular issue are published on the same date. For example: Volume 4, Issue 1 is a subcategory of issues (and ALL posts for that issue are contained in subcats of it).

    So what I want to do is pull the most recently published issue, and then be able to loop through all of the subcategories for that particular issue.

    I want to use this to pull up queries for:

    1. All posts in each subcat (by subcat) at once (for a table of contents)
    2. All posts in a single subcat (for single category sections)
    3. All posts and subcategories with posts in a single subcat (for example – there are numerous poetry sections combined under a “poems” category.
    Thread Starter johnnywhee

    (@johnnywhee)

    My logic is that if I can get the category ID for the most recently published issue – I can use that for my queries.

    Ideally, I’d like to have a variable for the current issue’s cat_ID (like $current_issue_ID)that I can write into query_posts, like this:

    <?php query_posts(‘orderby=name&order=asc&cat=.'<?php echo $current_issue_ID’.’); ?>

    or

    <?php wp_list_categories(‘title_li=&orderby=name&order=asc&child_of=.'<?php echo $current_issue_ID’.’); ?>

    Thread Starter johnnywhee

    (@johnnywhee)

    Anyone? I’m sure I just need to use implode to cast the variable to a string so I can use it in the query loops, but arrays really confuse me, as does syntax.

    Thanks!

    So your category hierarchy is: Issues -> Volume # Issue # -> Subcategory? Are the parent categories also assigned to each post, or are only the sub-subcategories assigned?

    Is the goal to create three custom queries? One that pulls all posts, one that pulls all posts in a single sub-subcategory, and one that pulls all posts that are…well…I’m not sure.

    Anyway, to grab an array of categories assigned to the last published post, something like this would do:

    $latest_post = current( get_posts( array( 'numberposts' => 1 ) ) );
    
    $categories = get_the_category( $latest_post->ID );

    An example of using the returned categories (assuming only one category assigned to each post):

    $latest_post = current( get_posts( array( 'numberposts' => 1 ) ) );
    $category = current( get_the_category( $latest_post->ID ) );
    
    $args = array(
        'orderby' => 'name',
        'order' => 'ASC',
        'category__in' => array( $category->cat_ID )
    );
    
    query_posts( $args );

    Edit: In response to the comment left on my site…

    The examples I’ve given should be working examples assuming I haven’t made any mistakes; I used an array of arguments instead of a query string. Specifically, category__in is a parameter you can use with any custom queries and doesn’t need to be replaced with anything. I did leave off the code to create a loop with your custom query though.

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Parameters

    You can keep posting here instead of going to my site. Sometimes I’m slow to post, but I do get all your follow-up posts here by email. ??

    Thread Starter johnnywhee

    (@johnnywhee)

    (Volume # Issue #) is assigned to each post, and there are general subcategories – for example the subcategory Poems has 7 subcategories.

    So for any particular issue:
    Volume # Issue #
    >Poems
    >>Poem type 1
    >>poem type 2
    >Fiction
    >>Short Fiction
    >>Long Fiction

    etc.

    I need to specify the latest post within the issues category. If I could cast the current issue ID into a variable, I could use it in the loops that I know how to use. You know, the ones where you can specify cat_id.

    I need to pull all subcategories & posts from the current issue to build a table of contents.

    I need to be able to pull a particular subcategory & posts from the current issue.

    I need to be able to pull a particular subcategory and its subcategories and posts from the current issue.

    Is that clear? I need wordpress to determine the current issue, and then I need to be able to query based on the current issue.

    The code I originally posted gives me the current issue. the problem is that I can’t substitute it into syntax that i understand.

    for example, this works:

    $categories = get_categories('child_of=33&number=1&order=desc');
    foreach ($categories as $category) {
    $currentissue1 = $category->cat_ID;
    $currentissue2 = $category->cat_name;
    }

    When I enter this:

    <?php echo $currentissue1; ?>

    It displays the current cat_ID. But it doesn’t work if I do this:

    <?php wp_list_categories('orderby=name&show_count=0&use_desc_for_title=0&child_of='.$currentissue1;.'');

    Substituting a variable into a query structure i understand would be infinitely more accessible for me than trying to learn new PHP syntax, though I’m willing to try. I attempted the code samples posted, but to no avail. I just get PHP errors.

    Mokies. Since it’s set-up the way you want and everything is working, the only problems seem to be with your argument string in wp_list_categories(). Try this instead:

    wp_list_categories( 'orderby=name&show_count=0&use_desc_for_title=0&child_of=' . $currentissue1 );
    Thread Starter johnnywhee

    (@johnnywhee)

    Awesome!

    Once again I’m rendered powerless by a single semicolon.

    Really appreciate your help. Thank you!

    Thread Starter johnnywhee

    (@johnnywhee)

    OK! That worked like a charm – NOW I definitely need help with looping through all the categories and posts to create a Table of contents of the posts.

    The thing is – All the posts for a current issue belong to a particular cat_ID AND another category.

    So what I need is a loop that grabs all the posts in a particular category, and then lists all the posts under their other respective categories.

    So, let’s say I have a post in a category named Issue 7 AND also Editor’s Notes, and two posts in Issue 7 AND Short Stories.

    I need to show something like this in the TOC:

    Issue 7
    >Editor’s Notes
    >>Title of and link to post

    >Short Stories
    >>Title and link to Short Story 1
    >>Title and link to Short Story 2

    Any help for this?

    Thanks!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Query based on current issue – how to?’ is closed to new replies.