WordPress.org

Forums

Intuitive Custom Post Order
[resolved] Over-ride CPO in shortcode WP_query (4 posts)

  1. wgstjf
    Member
    Posted 1 year ago #

    Hi there,

    Love the plugin but have just come up against an issue. I have a custom shortcode that gets a list of testimonials (from a custom post type). I need the shortcode to be able to get the relevant posts in random order. I can do this with CPO disabled but not when active. The query is currently:

    $args = array(
    	'showposts'   		=> $count,
    	'post_type'        	=> 'testimonial',
            'orderby'			=> 'rand',
    	);
    
    	$testimonial = new WP_Query($args);

    Can you tell me how to ensure that the orderby arg is obeyed and not overridden by CPO?

    Cheers,

    Will

    https://wordpress.org/plugins/intuitive-custom-post-order/

  2. SpankMarvin
    Member
    Posted 1 year ago #

    Hey Will

    Not sure if you're having this problem still, but I have a solution for you to try. Mine comes from the need to orderby => meta_value only under the condition that it was defined in the shortcode. In the context of my site, this meant that if a shortcode called a surname parameter in it's listing of people, the order should be by surname instead of drag n drop. However, this was being overridden by the plugin no matter what my arguments for the query.

    So, I disabled the filter called by the plugin class, only if the parameter was called. Here's the removal code:

    global $hicpo; // Call the class variable for the ICO plugin, so we can disable its overriding of the orderby parameter
    remove_filter( 'pre_get_posts', array( $hicpo, 'hicpo_pre_get_posts' ) );

    Hope this helps,

    John

  3. wgstjf
    Member
    Posted 1 year ago #

    John,

    Outstanding solution, many thanks!

    In case anyone else is interested. This does work! The only thing you need to do is specify the orderby and order in your args whenever you query the posts as adding the above solution removes the plugin's default values of

    'orderby'	=> 'menu_order',
    'order'			=> 'ASC',

    An example might be that you have a slider for testimonials that you want a random selection to be used but on the testimonials page you want them to appear in the order specified in the admin area.

    Thanks again John and all the best,

    Will

  4. hijiri
    Member
    Plugin Author

    Posted 6 months ago #

    Hi, there.

    It works with the code below.
    Custom query which uses 'order' or 'orderby' parameters is preferred.

    $query = new WP_Query( 'orderby=rand' );

    Is problem another?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Intuitive Custom Post Order
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic