10

I have a question that I would like to ask on WPSE, but fear that it leans towards the "Bad Subjective" end of Good Subjective, Bad Subjective spectrum.

So, I thought I'd ask here whether it would be appropriate to ask it on WPSE. If not appropriate, is there a way I could alter the question so that it would be appropriate?

Question I'd like to ask on WPSE

WP provides a large number of filters/actions to alter the query performed by WP_Query.

There is ample (if not always adequate) documentation on individual filters in the Developer Resources (e.g., pre_get_posts, parse_request, request, etc).

And the Filter Reference and Action Reference in the codex goes a little way to documenting which filters/actions affect which phase of processing.

To futher complicate matters, in many cases, the same result can be achieved by using different filters/actions.

For example, suppose I want to change the default sorting of posts (including CPTs) on the edit.php screen. I can accomplish that goal in (at least) 2 different ways:

using the request filter

add_filter ('request', 'set_default_edit_orderby_using_request') ;

function
set_default_edit_orderby_using_request ($query_vars)
{
    global $pagenow, $typenow ;

    if ('edit.php' != $pagenow || 'my_cpt' != $typenow || isset ($query_vars['orderby'])) {
        return ($query_vars) ;
        }

    $query_vars['meta_key'] = 'some_meta_key' ;
    $query_vars['orderby'] = 'meta_value menu_order title' ;
    $query_vars['order'] = 'ASC' ;

    return ($query_vars) ;
}

using the pre_get_posts action

add_action ('pre_get_posts', 'set_default_edit_orderby_using_pre_get_posts') ;

function
set_default_edit_orderby_using_pre_get_posts ($query)
{
    global $pagenow, $typenow ;

    $orderby = $query->get ('orderby') ;
    if ('edit.php' != $pagenow || 'my_cpt' != $typenow || !empty ($orderby)) {
        return ;
        }

    $query->set ('meta_key', 'some_meta_key') ;
    $query->set ('orderby', 'meta_value menu_order title') ;
    $query->set ('order', 'ASC') ;

    return ;
}

After much searching (here on WPSE, on the wordpress.org forums and on google), I have yet to find a good reference for best practices on which WP_Query-related filter(s)/action(s) to use to accomplish a given task when more than 1 will do the job.

I'm looking for resources that would say things like:

when X and Y would both accomplish Z, prefer X because it is less expensive (in time/memory/etc)

Clarification of the question I'd like to ask

Based on the answers I've gotten so far, I think I need to clarify the question I'd like to ask on WPSE.

I'm not really asking whether request or pre_get_posts is "best" for the example use case in my question (to set the default orderby for the edit.php screen). That was just an example to set the stage.

What I'm asking for is suggestions for "good" reference material (e.g., books, blog posts, conference presentations, etc) that would help to answer (or at least offer lists things to one should consider when trying to answer) a whole range of questions about which WP_Query-related hook to use for a whole range of use cases (when multiple hooks would produce the results.

Why I think the question might be "Bad Subjective" is that what 1 person things is a "good" discussion on top might be considered "bad" by another.

3 Answers 3

7

Usually broad topics like "Best Practices" are closed as being opinionated or too broad but I've been seeing them more and more. I think a better way to ask is Should I be using pre_get_posts or request for xyz which would boil down to a singular answer.

I doubt you'll find a suitable large reference of using Hook A over Hook B you'll have to test your specific use case and read the docs to figure out which hook best suits your needs. If at that point you're still unsure then would be the time to ask the question of A or B here.

Per The Edit

The type of question you're referring to would fall under any of these off topic categories:

  • Too Broad of a topic to get a solid single answer on.
  • Primary Opinionated since everyone may have different reliable resources.
  • Asking for recommendations for plugins, themes, books or hosting services, in this case would be "recommended resources" which hits both the above.

You hit the nail on the head with your initial assertion that...

[...] what 1 person things is a "good" discussion on top might be considered "bad" by another.

To top this off, domains change hands from time to time, links may eventually expire, or in rare cases redirect to malicious content of which we cannot keep track of on a continuous basis. So having a single answer full of external links to possible resources is a can of worms trouble.

In the end it would most likely be considered to be off-topic and closed.


Where can I ask these things?

WordPress is a pretty big ecosystem and one of the most helpful communities I've run across. Just because this type of question is disallowed in this format doesn't mean you shouldn't ask.

We have "The Loop" chat which is populated.

There's other IRC Chatrooms which are always bustling.

There's the WordPress forums and at least 1 WordPress group on every social site.

5
  • see the clarification I just added in my edited question. Commented Mar 6, 2017 at 23:17
  • and I suspect you are correct that I'm not likely to "find a suitable large reference" (otherwise I probably would have already found it), but that's why I'd like to ask the question Commented Mar 6, 2017 at 23:18
  • @Paul'SparrowHawk'Biron I think you hit the nail on the head with your last paragraph. I've updated my answer to reflect your current changes.
    – Howdy_McGee Mod
    Commented Mar 6, 2017 at 23:41
  • 1
    your revised answer pretty much sums what I thought the response would be. In the future, when I do have a specific question of this type (as opposed to my current "improve my WP chops while I've got some down time between projects" situation) I will be sure to phrase it more like what @kaiser suggests in his answer. Commented Mar 6, 2017 at 23:57
  • Sounds good. Thanks for coming here for clarification!
    – Howdy_McGee Mod
    Commented Mar 7, 2017 at 0:33
4

"Best" type of questions are always the worst questions. The more you know the less it becomes clear that there is a one and only "best". "Best" can be performance related, memory usage related, code readability, and for each aspect "best" might be different.

For me, any code which does what is supposed to do is already "best", therefor for being useful a "best" question should contain a lot of context that will explain explicitly or implicitly why anyone should care. Most "best" question that I saw don't even try to supply minimal context and assume that there is kind of simple black/white answer, something that is rarely true for any non trivial question.

1
4

Let's rephrase your question:

Under consideration of X and Y, is A or B the better solution regarding N ?

Now X and Y could be things like high traffic web site and low memory shared host, while A and B might be your filter. N could be the result your are aiming for.

The problem that still would be there is that you can totally perform the research yourself at this point. While the question would be appropriate, it still is not a good question. It might be good if you leave the door open for different solutions and provide a basic test setup that people could implement easily—keeping in mind that 30 min. is pretty much the top I would expect from people to invest in an (unpaid) answer.

On a personal side, you would still retrieve an upvote from me for showing your research.

Answer: Yes, ask your question, but rephrase it upfront. You can always answer it yourself—we even encourage people to do so and share the knowledge they gain during their research. Ask and Answer does not mean, that there need to be other people involved than yourself. The site works pretty fine as personal knowledge base, partly because its well indexed and partly because you will forget what you once knew and will thank yourself for putting it here.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .