Forum Replies Created

Viewing 3 replies - 1 through 3 (of 3 total)
  • Forum: Hacks
    In reply to: Changing displayed post
    Thread Starter _F12

    (@_f12)

    Yeah, I don’t think I’ll have too much trouble defining the data structures and setting up the UI around that (although, as you said, it will be time-consuming).

    The problem I am having is still around the URL and communicating the post to show. I can set the post-id given a post-type, but I still can’t figure out how to have a URL like /competencies/4 and have that actually show the desired competency. The first issue is decoding the URL to something that I can interpret and perform logic based on (first, calling the displaying the relevant page-template for competencies, then querying the post which corresponds to 4 for that user). The best I can achieve at the moment is redirecting the user to a specified post if they go to any other post (also changes the header location for some reason rather than just changing the displayed post).

    function query_competency($query ) {
    	if ($query->get('post_type') == 'comp_competencies') {
    		$query->set('p', 43); //Logic to decide which post to be added later
    	}
    }
    
    add_action( 'pre_get_posts', 'query_competencies' );

    The above is the code I’m using to try and build an understanding at this preliminary stage. All it can do at the moment is, if I’m viewing any post singly that is of the specified post_type, then it redirects me to the URL of the post with id=43.

    I suspect I need to play around with the mod_rewrite rules to get it to interpret my URL input but even after that I am still stuck for what to be doing, even if I set the permalink rules to default (with standard GET vars) for the time being.

    Forum: Hacks
    In reply to: Changing displayed post
    Thread Starter _F12

    (@_f12)

    Doiii, I don’t know what I was thinking. Of course, thanks.

    I’m at a bit of a loss now for how best to approach this problem. I’ll try to explain what I want to achieve as best I can.

    Basically each person has a certain list of competencies that they need to go through, represented by a custom post-type. So essentially I can have a database field which represents the competencies they need and their order, and then retrieve which post I should be showing based off two things: the user_id and the number they have reached.

    So let’s say user “mwahlberg” is on stage 3, the database tells us to show him the post (custom post-type) with id=523. Ideally I would have as the link either site.com/competency/3 (pulling up id=523 internally, rather than in the URL) or similar. For another user though, “jtimberlake”, that same permalink would pull up id=445 because the database has a different set of required competencies for him. Obviously the whole set would be restricted to registered users (not a problem for this specific application).

    What is the best way to achieve this? I can manage it using some GET variables in the function but I think there is a better more abstracted way to do it that goes along more with the WordPress way of doing things. Can anyone give me some pointers on where to look/read to get started?

    Forum: Hacks
    In reply to: Changing displayed post
    Thread Starter _F12

    (@_f12)

    Gotcha. I’ve been having a look over pre_get_posts and the related documentation pages but I’m still not sure I’ve got a handle on it.

    function query_question( $query ) {
    	$var = 42 //Set based on other logic in real case
    	$query->set ('p', $var);
    }
    
    add_action( 'pre_get_posts', 'query_question' );

    I’ve got the above code in the template for my custom post_type (single-post_type.php), but it is still just displaying the post to which the permalink refers rather than the post with id = 42. (Obviously the 42 is just a placeholder at the moment so I can get a proof of concept, then I will add in the other logic). Am I completely misunderstanding the functions and WP_Query here?

    As far as SEO it is not a big concern for this specific section of the blog.

Viewing 3 replies - 1 through 3 (of 3 total)