• Resolved tbp278

    (@tbp278)


    I’m building a custom meta box for inclusion on a custom post type page (‘press_releases’) which feeds in a list of recently posted items from a separate custom post type (‘videos’).

    The idea is that the user can select which videos are displayed in the side panel of that particular press release.

    I’ve added the meta box in standard way:

    add_meta_box('post_info3', 'Select media', 'media_selection_box', 'press_releases', 'side', 'high');

    In the added meta box I’m running a custom query to call a list of posts from the ‘video’ post type. It’s all working good except for 1 weird little problem.

    When I run this query:

    $args = array (
    
    		'post_type' 	=> 	'video',
    		'artists_tax'	=>	$artist_name
    
    	);
    
    	$media_query = new WP_Query($args);

    in the meta box, it beings back the correct posts from the video post type, but it also changes the title and body content of the main post to the title of the last entry brought back by the query.

    It also changes the slug of the post from:

    https://domain.com/press_release/post-title

    to:

    https://domain.com/video/post-title

    Obviously this query is causing a bit of conflict somewhere. I’ve tried running wp_reset_query but to no avail.

    Has anyone had this problem before? Any ideas how to get round it?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter tbp278

    (@tbp278)

    I’ve temporarily got round the problem by running another query directly after the loop has closed which queries the correct post type.

    $args = array (
    
    	'post_type' 	=> 	'press_releases',
    
    );
    
    $media_query = new WP_Query($args);

    It’s solved it but it’s pretty ugly. Is there another way to reset the query back to its original state? wp_reset_query seems to have no effect.

    Maybe the problem is with using the $args variable, try renaming your args variable to something else..

    eg.

    $my_args

    Thread Starter tbp278

    (@tbp278)

    Tried it and it doesn’t make a difference.

    I think I need to save the original query which brings back the post data, run my custom query, then re-run the original query.

    …Something like that anyway.

    Only problem is I can’t figure out how to save the original query – $wp_query is empty before the custom query runs

    Try globalising the variable.

    eg.

    global $wp_query;

    Then see if it has data..

    Thread Starter tbp278

    (@tbp278)

    Yeah i did that too.

    I’ve re-written it now to load via ajax. It solves the problem and actually works better in this case.

    Didn’t get a fix though, but thanks for your suggestions all the same

    Would need to see more of the code to narrow down the original issue, but glad to hear you found a solution all the same… ??

    I’d personally opt for the ajax to, just make sure you setup nonces for your ajax calls, or use no-priv ajax.. (need to check docs for specifics i’m no expert in the ajax stuff – just speaking in terms of being secure with your ajax).

    For ref:
    https://codex.www.ads-software.com/AJAX
    https://codex.www.ads-software.com/AJAX_in_Plugins

    I came across the same issue. It’s pretty easy to resolve though. Rather than using WP_Query you can just make a call with get_posts() and directly play with the array data rather than relying on the loop helpers. This gets around the query issues.

    I’ve now got a couple of meta boxes that list particular groups of posts or custom post types. And wordpress is now even more fantastic!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Running custom queries is admin meta boxes’ is closed to new replies.