1

UPDATE:

The hook is working with "classic" Wordpress filter (like post__in), so it's probably a Posts2Posts issue. I'm still looking for suggestions if someone has any idea.

ORIGINAL POST

I'm trying to get all posts displayed by issue number in archive pages. Issues are a custom post type (linked with post2post, which is why I use parse_query instead of pre_get_post)

<?php

function categories_by_issue ($query) {

    if (! $query->is_category() || ! $query->is_main_query()) return;

    $query->set( 'connected_type', 'issue_to_posts');
    $query->set( 'connected_items', 71); // 71 needs to be dynamic
    $query->set( 'nopaging', true);
}

add_action('parse_query', 'categories_by_issue');
?>

This action hook works fine! Problem is: I need to get the issue ID with another query. (ID and issue number are of course different, my '#3' issue has '71' ID)

I tried to add a WP_Query in my hook, which basically break the whole thing. I can get my issue number, but the main query finally shows all posts.

<?php

function categories_by_issue ($query) {

    if (! $query->is_category() || ! $query->is_main_query()) return;

    $issues = new WP_Query ( /* ... */);
    $issue_id = $issues->get_posts()[0]->ID;
    var_dump($issue_id); // -> 71 : My ID is correct

    $query->set( 'connected_type', 'issue_to_posts');
    $query->set( 'connected_items', $issue_id);
    $query->set( 'nopaging', true);

}

add_action('parse_query', 'categories_by_issue');
?>

Is there a way I could request some information in my hook, ideally without involving direct SQL request?

1

You are invoking an infinite loop, because you are calling WP_Query inside WP_Query, which is where the hook resides, so you are hooking it into parse_query over and over again. To avoid it, put the following at the beginning of your callback function:

// avoid infinite loop
remove_action( 'parse_query', __FUNCTION__ ); 


Additional note, it might just be a lesser technicality, but in my mind the right hook to use would be pre_get_posts.

2
  • Hi ! Thanks, but I think the infinite loop is avoid by "$query->is_main_query()". Removing the action hook doesn't actually change anything with my issue :/
    – Jérémie
    Jun 19 '15 at 8:41
  • Your conditions do not prevent all cases, just some, but in those cases that apply you are still invoking the infinite loop, so remove the callback from the hook to avoid unintended problems. Since the static version of your code is working, I personally can't spot the problem from the information available. One thing I noticed is, you could/should run WP_Query with the fields parameter set to ids - 'fields' => 'ids' -, so it returns an array with the ID, which you should be able to use directly. @Jérémie
    – Nicolai
    Jun 19 '15 at 11:18
1

I couldn't solve this issue but found a workaround : I get my issue ID on parse_request hook and use it in parse_query (I created a class with an $issue_id var, but global variable should be fine too).

Still don't know why you can't use WP_Query directly in parse_query, but I opened ticket on Posts2Posts github : https://github.com/scribu/wp-posts-to-posts/issues/483

0

I din't try this. But what about making a function for fetching the issue_id? And remember, wp_reset_postdata() for a WP_Query() is important.

function get_the_issue_id() {
  $issues = new WP_Query( /* .... */ );
  $issue_id = $issues->get_posts()[0]->ID;
  wp_reset_postdata(); //it's important
  return (int) $issue_id;  
}

Now do the parse_query thing:

function categories_by_issue ($query) {    
    if (! $query->is_category() || ! $query->is_main_query()) return;

    $query->set( 'connected_type', 'issue_to_posts');
    $query->set( 'connected_items', get_the_issue_id() ); //using the function
    $query->set( 'nopaging', true);    
}    
add_action('parse_query', 'categories_by_issue');
1
  • It's cleaner ! Thanks ! But still doesn't work :/ Actually I tried with 'post__in' instead of 'connected_type' and it seems OK. It's probably an issue with the Posts2Posts hooks. I'm checking
    – Jérémie
    Jun 18 '15 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.