Can network_query_post use all WP_Query parameters and normal template tags?

Hello,

1.What I'm wondering is if I can use the same parameters with the network_query_post as I can with WP_query?

2.Also can I use template tags that include get_post_meta(); along with some custom plugin template tags and so on in the network_query_post?

Here are my parameters that I'm using with WP_Query:

//orders by user liked content

'order_by' => 'meta_value_num',

'post__in' => get_user_option( 'liked_posts_code', get_current_user_id() )

$args = array( 'posts_per_page' => 6, 'post_type' => 'post-a', 'user_id' => get_fa());

Thank you!

  • Vinod Dalvi

    Hi @Corbin,

    I hope you are well today and thank you for your question.

    1.What I'm wondering is if I can use the same parameters with the network_query_post as I can with WP_query?

    I checked the Post Indexer plugin code and found it supports almost all parameters of WP_query class.

    You can also find which parameters are supported in the functions fill_query_vars and &get_posts int he following Post Indexer plugin file.

    post-indexer/classes/networkquery.php

    If you face any problem using any parameter then please let us know.

    2.Also can I use template tags that include get_post_meta(); along with some custom plugin template tags and so on in the network_query_post?

    In network query we have to query posts along with blog id as we are iterating over posts from different blogs in this case the default WordPress functions doesn't work out of the box as they only query current blog posts so we have developed special functions like network_get_post_custom( $blog_id, $post_id ) to query it.

    You will find these functions on the following Post Indexer plugin files.

    post-indexer/includes/functions.php
    post-indexer/classes/networkquery.php

    If you want to use default WordPress functions like get_post_meta() then you have to switch to and restore from the respective blogs using functions switch_to_blog and restore_current_blog

    Please advise if you have more questions.

    Best Regards,
    Vinod Dalvi

    • Corbin

      Great advice Vinod,

      I was poking around post-indexer/includes/functions.php and found that the post indexer functions are using switch_to_blog to get things like the permalinks and so on from another site on the network.

      My main question I believe is now is:

      switch_to_blog( ); is said to be an "expensive to use" query on the front end by the wordpress codex. Is this why it's used to output specific functions in the class network_query_posts(); instead of just using the WP_Query class and switch to blog to run the whole query?

      My secondary question would be:

      Should I just build a function name it something like network_get_post_meta(); for each custom field I want to get, or does network_get_post_custom( ); work in the same way as get_post_meta(); does?

      Eg

      // Gets custom field in wp query loop
      echo get_post_meta( get_the_ID(), 'custom_field_stuff', true );
      //Gets custom field in network_query_posts
      echo network_get_post_custom( get_the_ID(), 'custom_field_stuff', true  );

      What does Wordpress codex mean by: "It's too expensive a query to run on the front end."?
      Thank you very much for your help!

  • Vinod Dalvi

    Hi @Corbin,

    Thank you for your reply.

    switch_to_blog( ); is said to be an "expensive to use" query on the front end by the wordpress codex. Is this why it's used to output specific functions in the class network_query_posts(); instead of just using the WP_Query class and switch to blog to run the whole query?

    I don't understand which functions you are referring which is used in place of WP_Query class.

    Could you please explain your question a bit more in detail so that i can answer it?

    Should I just build a function name it something like network_get_post_meta(); for each custom field I want to get, or does network_get_post_custom( ); work in the same way as get_post_meta(); does?

    As said previously you have to call the function network_get_post_custom( $blog_id, $post_id ) which calls internally the function get_post_meta for the particular blog which returns all meta values in an associative array and not specific meta value.

    What does Wordpress codex mean by: "It's too expensive a query to run on the front end."?

    I think the information provided on the following pages will help you to understand it.

    http://scotty-t.com/2012/03/13/switch_to_blog-is-an-unfunny-nightmare/
    http://wordpress.stackexchange.com/a/106862

    Regards,
    Vinod Dalvi

    • Corbin

      Hey Vinod,

      switch_to_blog( ); is said to be an "expensive to use" query on the front end by the wordpress codex. Is this why it's used to output specific functions in the class network_query_posts(); instead of just using the WP_Query class and switch to blog to run the whole query?

      I don't understand which functions you are referring which is used in place of WP_Query class.

      Could you please explain your question a bit more in detail so that i can answer it?

      Sorry I really didn't write that well. Even to the point where I'm having trouble trying to figure out what I said lol.

      I saw that in post-indexer/includes/functions.php switch_to_blog is used to return the permalink.

      function network_get_permalink( $blog_id = 0, $id = 0 ) {
      
      	$post = &network_get_post( $blog_id, $id );
      
      	if(!empty($post)) {
      		switch_to_blog( $post->BLOG_ID );
      		$permalink = get_permalink( $post->ID );
      		restore_current_blog();
      
      		return $permalink;
      	}
      }

      Since the only cross site querying I will be doing will be querying from my mainsite to the rest of my subsites. I was trying to figure out if I should use post indexer, or just use wp_query and switch to blog. After reading about switch_to_blog in the wordpress codex (It being an expensive function), I was starting to wonder if it would be more beneficial to use the post indexer plugin with the network_post_query and switch_to_blog to return certain template tags like above. Rather than just using WP_Query and switch_to_blog.

      So basically, is it more "expensive" to use switch_to_blog in WP_Query than it is the way WPMU has used it to just get certain functions like the permalinks?

      After reading the reading the following quote in the "switch to blog is an unfunny nightmare" article you had posted. I think I may be okay because I'm only querying one site (my mainsite) onto all of my subsites. What do you think?

      If your site switches from one blog to the next and doesn’t intermingle content – you really don’t have much to worry about after your initial setup. But in almost all cases, if you want to start using switch_to_blog() to co-mingle content from multiple sites inline, get ready to do some debugging!

      Thank you for your help so far!

      • Vinod Dalvi

        Hi @Corbin,

        I was starting to wonder if it would be more beneficial to use the post indexer plugin with the network_post_query and switch_to_blog to return certain template tags like above. Rather than just using WP_Query and switch_to_blog.

        So basically, is it more "expensive" to use switch_to_blog in WP_Query than it is the way WPMU has used it to just get certain functions like the permalinks?

        You can achieve the same result using WP_Query and switch_to_blog or using post indexer plugin and its functions but the difference is in the performance as post indexer plugin saves some data in its custom tables( 'network_posts', 'network_rebuildqueue', 'network_postmeta', 'network_terms', 'network_term_taxonomy', 'network_term_relationships', 'network_log' etc. ) retrieved from different blogs which it uses instead of retrieving it directly from blogs except for some cases like retrieving permalink which can be different from each blog.

        After reading the reading the following quote in the "switch to blog is an unfunny nightmare" article you had posted. I think I may be okay because I'm only querying one site (my mainsite) onto all of my subsites. What do you think?

        I also think so and you can also use caching to improve the performance of it.

        Thanks,
        Vinod Dalvi

  • Corbin

    I would like to add the post thumbnail to the functions as well, along with a few custom template tags. Would this look like what I would have to create for each template tag?

    function network_post_thumbnail( $blog_id = 0, $id = 0 ) {
    
    	$post = &network_get_post( $blog_id, $id );
    
    	if(!empty($post)) {
    		switch_to_blog( $post->BLOG_ID );
    		$postthumbnail = get_the_post_thumbnail( $post->ID );
    		restore_current_blog();
    
    		return $postthumbnail;
    	}
    }

    Thanks for your advice.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.